1 DATA

1.1 Load data

## Longitudinal data
data_sum <- loadlongitudinaldata(dataset = "DATA_Adults_G1G29.csv", rm_generation1 = 1,rm_generation2 = 7,rm_generation3 = 29)

## Phenotyping steps
data_G0 <- loadfitnessdata(dataset = "Selection_Phenotypage_G0_G7_G8.csv", generation = "G1")
data_G7 <- loadfitnessdata(dataset = "Selection_Phenotypage_G0_G7_G8.csv", generation = "G7")
data_G29 <- loadfitnessdata(dataset = 
                            "PERFORMANCE_Comptage_adultes_G13G14G15G16G17G18G19G20G21G22G23G24G25G26G27G28G29.csv",
                            generation = "29")
head(data_sum)
##   Line Fruit_s Generation         Phase  N Nb_adults       sd    fitness
## 1  CE1  Cherry          2 first_prepool 20   9.15000 6.123939 -0.7819784
## 2  CE1  Cherry          3 first_prepool 10  15.30000 7.631077 -0.2678794
## 3  CE1  Cherry          4 first_prepool  8  15.00000 7.782765 -0.2876821
## 4  CE1  Cherry          5 first_prepool  6  14.50000 6.284903 -0.3215836
## 5  CE2  Cherry          2 first_prepool 20   7.75000 7.758696 -0.9480394
## 6  CE2  Cherry          3 first_prepool  7  14.42857 8.303757 -0.3265219
##   se_fitness
## 1  0.1496562
## 2  0.1577228
## 3  0.1834415
## 4  0.1769518
## 5  0.2238577
## 6  0.2175215
head(data_G0)
##     Treatment Line Fruit_s Nb_eggs Nb_adults SA Emergence_rate
## 993    Cherry  CE1      GF      76         6  0     0.07894737
## 994    Cherry  CE1      GF      89        17  0     0.19101124
## 995    Cherry  CE1      GF      57        12  0     0.21052632
## 996    Cherry  CE1      GF     172        24  0     0.13953488
## 997    Cherry  CE1      GF     173        33  0     0.19075145
## 998    Cherry  CE1      GF      91        18  0     0.19780220
head(data_G7)
##    Treatment Line    Fruit_s Nb_eggs Nb_adults SA Emergence_rate
## 3 Strawberry  CR4  Cranberry     152        68  0      0.4473684
## 4  Cranberry  CR4  Cranberry     246        25  1      0.1016260
## 5     Cherry  CR4  Cranberry     238        29  0      0.1218487
## 6     Cherry  CR4  Cranberry     166        23  0      0.1385542
## 8  Cranberry  FR3 Strawberry     204         5  0      0.0245098
## 9 Strawberry  FR3 Strawberry     124        45  1      0.3629032
head(data_G29)
##       Treatment Line Fruit_s Nb_eggs Nb_adults SA Emergence_rate
## 5392 Strawberry  CEA  Cherry     196        16  0     0.08163265
## 5393 Strawberry  CEA  Cherry     192        30  0     0.15625000
## 5394 Strawberry  CEA  Cherry     160        17  0     0.10625000
## 5395 Strawberry  CEA  Cherry     106         9  0     0.08490566
## 5396 Strawberry  CEA  Cherry     119        14  0     0.11764706
## 5397 Strawberry  CEA  Cherry     204        24  0     0.11764706
dim(data_G29)
## [1] 990   7
## Add line variable
levels(data_G0$Line) <- rep("Anc", nlevels(data_G0$Line))

## Combine datsets
data <- rbind(data_G0, data_G7, data_G29) 
data <- data.frame(Generation = c(rep("0", nrow(data_G0)), rep("7", nrow(data_G7)), rep("29", nrow(data_G29))), data, Obs= as.factor(1:nrow(data)))
head(data)
##     Generation Treatment Line Fruit_s Nb_eggs Nb_adults SA Emergence_rate Obs
## 993          0    Cherry  Anc      GF      76         6  0     0.07894737   1
## 994          0    Cherry  Anc      GF      89        17  0     0.19101124   2
## 995          0    Cherry  Anc      GF      57        12  0     0.21052632   3
## 996          0    Cherry  Anc      GF     172        24  0     0.13953488   4
## 997          0    Cherry  Anc      GF     173        33  0     0.19075145   5
## 998          0    Cherry  Anc      GF      91        18  0     0.19780220   6
## New variable for analyses
data$Generation_Fruit_s_Treatment <- as.factor(paste(data$Generation, data$Fruit_s, data$Treatment, sep="_"))
data$Line_Treatment <- as.factor(paste(data$Line, data$Treatment, sep="_"))
data$Treatmentrel <- relevel(data$Treatment, ref="Strawberry")

1.2 Compute fitess change per population

1.2.1 Small example with a plot of the likelihod difference as a function of each parameter value

mfitness <- MASS::glm.nb(Nb_adults ~ Treatment, data=data)
summary(mfitness)
## 
## Call:
## MASS::glm.nb(formula = Nb_adults ~ Treatment, data = data, init.theta = 1.909520807, 
##     link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.2900  -0.8197  -0.1146   0.4139   2.7450  
## 
## Coefficients:
##                     Estimate Std. Error z value Pr(>|z|)    
## (Intercept)          3.39580    0.03163 107.365   <2e-16 ***
## TreatmentCranberry  -0.05073    0.04470  -1.135    0.256    
## TreatmentStrawberry  0.02466    0.04471   0.552    0.581    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(1.9095) family taken to be 1)
## 
##     Null deviance: 1892.5  on 1673  degrees of freedom
## Residual deviance: 1889.5  on 1671  degrees of freedom
## AIC: 14468
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  1.9095 
##           Std. Err.:  0.0698 
## 
##  2 x log-likelihood:  -14460.0940
pr1 <- profile(mfitness, alpha = 0.1)

MASS:::plot.profile(pr1)

1.2.2 Fitness

mfitness <- MASS::glm.nb(Nb_adults ~ -1 + Treatment + Line:Treatment, data=data)
summary(mfitness)
## 
## Call:
## MASS::glm.nb(formula = Nb_adults ~ -1 + Treatment + Line:Treatment, 
##     data = data, init.theta = 2.366102685, link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.8419  -0.7673  -0.0402   0.4835   3.7740  
## 
## Coefficients:
##                             Estimate Std. Error z value Pr(>|z|)    
## TreatmentCherry              3.21848    0.06802  47.318  < 2e-16 ***
## TreatmentCranberry           3.26385    0.06789  48.077  < 2e-16 ***
## TreatmentStrawberry          3.35446    0.06764  49.590  < 2e-16 ***
## TreatmentCherry:LineCE1     -0.27404    0.22837  -1.200 0.230153    
## TreatmentCranberry:LineCE1  -0.91247    0.26333  -3.465 0.000530 ***
## TreatmentStrawberry:LineCE1  0.09553    0.24774   0.386 0.699777    
## TreatmentCherry:LineCE2     -0.56172    0.35753  -1.571 0.116160    
## TreatmentCranberry:LineCE2  -0.20358    0.40139  -0.507 0.612024    
## TreatmentStrawberry:LineCE2  0.22906    0.47936   0.478 0.632752    
## TreatmentCherry:LineCE3      0.18528    0.19923   0.930 0.352387    
## TreatmentCranberry:LineCE3  -0.11040    0.20830  -0.530 0.596097    
## TreatmentStrawberry:LineCE3 -0.01404    0.19953  -0.070 0.943891    
## TreatmentCherry:LineCE4      0.12791    0.31015   0.412 0.680033    
## TreatmentCranberry:LineCE4   0.46905    0.30646   1.531 0.125891    
## TreatmentStrawberry:LineCE4  0.58713    0.33925   1.731 0.083510 .  
## TreatmentCherry:LineCR1      0.63167    0.66973   0.943 0.345592    
## TreatmentCranberry:LineCR1   0.26984    0.34288   0.787 0.431298    
## TreatmentStrawberry:LineCR1 -0.46408    0.49363  -0.940 0.347144    
## TreatmentCherry:LineCR2      0.37884    0.34225   1.107 0.268337    
## TreatmentCranberry:LineCR2  -0.10685    0.28660  -0.373 0.709287    
## TreatmentStrawberry:LineCR2  0.23598    0.34224   0.690 0.490496    
## TreatmentCherry:LineCR3     -0.62165    0.17548  -3.543 0.000396 ***
## TreatmentCranberry:LineCR3   0.05810    0.16264   0.357 0.720928    
## TreatmentStrawberry:LineCR3  0.10658    0.16516   0.645 0.518711    
## TreatmentCherry:LineCR4      0.20904    0.30927   0.676 0.499096    
## TreatmentCranberry:LineCR4   0.35156    0.28201   1.247 0.212540    
## TreatmentStrawberry:LineCR4  0.52917    0.30532   1.733 0.083064 .  
## TreatmentCherry:LineCR5      0.53338    0.30635   1.741 0.081667 .  
## TreatmentCranberry:LineCR5   0.18840    0.26365   0.715 0.474855    
## TreatmentStrawberry:LineCR5  0.10344    0.34367   0.301 0.763430    
## TreatmentCherry:LineFR1      0.34257    0.14022   2.443 0.014561 *  
## TreatmentCranberry:LineFR1   0.35172    0.14181   2.480 0.013131 *  
## TreatmentStrawberry:LineFR1  0.33051    0.13626   2.426 0.015285 *  
## TreatmentCherry:LineFR2      0.35574    0.28238   1.260 0.207744    
## TreatmentCranberry:LineFR2  -0.16526    0.28734  -0.575 0.565206    
## TreatmentStrawberry:LineFR2  0.09023    0.28343   0.318 0.750229    
## TreatmentCherry:LineFR3      0.57926    0.30600   1.893 0.058361 .  
## TreatmentCranberry:LineFR3   0.17014    0.30917   0.550 0.582113    
## TreatmentStrawberry:LineFR3  0.13336    0.26329   0.507 0.612498    
## TreatmentCherry:LineFR4      0.72973    0.16699   4.370 1.24e-05 ***
## TreatmentCranberry:LineFR4   0.36284    0.17617   2.060 0.039432 *  
## TreatmentStrawberry:LineFR4  0.58063    0.17071   3.401 0.000671 ***
## TreatmentCherry:LineFR5      0.30788    0.67578   0.456 0.648677    
## TreatmentCranberry:LineFR5   0.39971    0.67297   0.594 0.552542    
## TreatmentStrawberry:LineFR5  0.18650    0.47999   0.389 0.697600    
## TreatmentCherry:LineCEA      0.14767    0.14094   1.048 0.294766    
## TreatmentCranberry:LineCEA  -0.21774    0.14241  -1.529 0.126279    
## TreatmentStrawberry:LineCEA  0.01971    0.14073   0.140 0.888596    
## TreatmentCherry:LineCEB      0.25038    0.14054   1.781 0.074831 .  
## TreatmentCranberry:LineCEB  -0.59200    0.14492  -4.085 4.41e-05 ***
## TreatmentStrawberry:LineCEB -0.15851    0.14152  -1.120 0.262699    
## TreatmentCherry:LineCEC      0.35854    0.14017   2.558 0.010528 *  
## TreatmentCranberry:LineCEC  -0.20671    0.14235  -1.452 0.146475    
## TreatmentStrawberry:LineCEC  0.21131    0.14002   1.509 0.131258    
## TreatmentCherry:LineCRA      0.32538    0.14028   2.320 0.020366 *  
## TreatmentCranberry:LineCRA  -0.14885    0.14204  -1.048 0.294660    
## TreatmentStrawberry:LineCRA -0.29731    0.14224  -2.090 0.036591 *  
## TreatmentCherry:LineCRB      0.16930    0.14086   1.202 0.229393    
## TreatmentCranberry:LineCRB   0.29720    0.14016   2.120 0.033966 *  
## TreatmentStrawberry:LineCRB -0.24539    0.14196  -1.729 0.083870 .  
## TreatmentCherry:LineCRC      0.08474    0.14121   0.600 0.548430    
## TreatmentCranberry:LineCRC   0.02704    0.14120   0.191 0.848148    
## TreatmentStrawberry:LineCRC -0.28951    0.14219  -2.036 0.041748 *  
## TreatmentCherry:LineCRD      0.54736    0.13959   3.921 8.81e-05 ***
## TreatmentCranberry:LineCRD  -0.15628    0.14208  -1.100 0.271367    
## TreatmentStrawberry:LineCRD -1.02555    0.14802  -6.928 4.26e-12 ***
## TreatmentCherry:LineCRE     -0.10496    0.14211  -0.739 0.460165    
## TreatmentCranberry:LineCRE   0.72513    0.13897   5.218 1.81e-07 ***
## TreatmentStrawberry:LineCRE -0.16810    0.14157  -1.187 0.235050    
## TreatmentCherry:LineFRA     -0.50599    0.14466  -3.498 0.000469 ***
## TreatmentCranberry:LineFRA  -0.25159    0.14260  -1.764 0.077692 .  
## TreatmentStrawberry:LineFRA  0.15709    0.14021   1.120 0.262542    
## TreatmentCherry:LineFRB      0.19377    0.14076   1.377 0.168632    
## TreatmentCranberry:LineFRB   0.04304    0.14113   0.305 0.760404    
## TreatmentStrawberry:LineFRB  0.56023    0.13903   4.030 5.59e-05 ***
## TreatmentCherry:LineFRC      0.32152    0.14029   2.292 0.021918 *  
## TreatmentCranberry:LineFRC   0.52110    0.13948   3.736 0.000187 ***
## TreatmentStrawberry:LineFRC  0.36641    0.13954   2.626 0.008643 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(2.3661) family taken to be 1)
## 
##     Null deviance: 97071.5  on 1674  degrees of freedom
## Residual deviance:  1897.6  on 1596  degrees of freedom
## AIC: 14287
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  2.3661 
##           Std. Err.:  0.0906 
## 
##  2 x log-likelihood:  -14129.0600
CIfitness <- confint(mfitness)

pr1 <- profile(mfitness, alpha = 0.1)

# pdf(file="figures/Profile.pdf")
# MASS:::plot.profile(pr1)
# dev.off()

1.2.3 Fecundity

mfecundity <- MASS::glm.nb(Nb_eggs ~ -1+Treatment+Line:Treatment, data=data)
summary(mfecundity)
## 
## Call:
## MASS::glm.nb(formula = Nb_eggs ~ -1 + Treatment + Line:Treatment, 
##     data = data, init.theta = 8.2113987, link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -5.7949  -0.6864  -0.0232   0.5593   3.0051  
## 
## Coefficients:
##                             Estimate Std. Error z value Pr(>|z|)    
## TreatmentCherry              5.00723    0.03584 139.699  < 2e-16 ***
## TreatmentCranberry           4.81096    0.03604 133.472  < 2e-16 ***
## TreatmentStrawberry          4.68065    0.03620 129.294  < 2e-16 ***
## TreatmentCherry:LineCE1      0.21744    0.11833   1.838 0.066115 .  
## TreatmentCranberry:LineCE1   0.39304    0.13118   2.996 0.002734 ** 
## TreatmentStrawberry:LineCE1  0.48342    0.13133   3.681 0.000232 ***
## TreatmentCherry:LineCE2      0.55345    0.18081   3.061 0.002206 ** 
## TreatmentCranberry:LineCE2   0.43255    0.20893   2.070 0.038430 *  
## TreatmentStrawberry:LineCE2  0.27518    0.25636   1.073 0.283097    
## TreatmentCherry:LineCE3      0.18914    0.10525   1.797 0.072338 .  
## TreatmentCranberry:LineCE3   0.33605    0.10924   3.076 0.002096 ** 
## TreatmentStrawberry:LineCE3  0.43781    0.10554   4.148 3.35e-05 ***
## TreatmentCherry:LineCE4      0.29906    0.16320   1.832 0.066879 .  
## TreatmentCranberry:LineCE4   0.27910    0.16397   1.702 0.088737 .  
## TreatmentStrawberry:LineCE4  0.69925    0.18141   3.855 0.000116 ***
## TreatmentCherry:LineCR1     -0.27103    0.36310  -0.746 0.455399    
## TreatmentCranberry:LineCR1   0.07562    0.18339   0.412 0.680090    
## TreatmentStrawberry:LineCR1  0.23568    0.25664   0.918 0.358456    
## TreatmentCherry:LineCR2      0.17033    0.18205   0.936 0.349452    
## TreatmentCranberry:LineCR2   0.24740    0.15052   1.644 0.100242    
## TreatmentStrawberry:LineCR2  0.19455    0.18348   1.060 0.288993    
## TreatmentCherry:LineCR3      0.28872    0.08921   3.236 0.001210 ** 
## TreatmentCranberry:LineCR3   0.44703    0.08571   5.216 1.83e-07 ***
## TreatmentStrawberry:LineCR3  0.34126    0.08792   3.882 0.000104 ***
## TreatmentCherry:LineCR4      0.16779    0.16362   1.025 0.305142    
## TreatmentCranberry:LineCR4   0.48150    0.14978   3.215 0.001306 ** 
## TreatmentStrawberry:LineCR4  0.32330    0.16434   1.967 0.049161 *  
## TreatmentCherry:LineCR5      0.08652    0.16391   0.528 0.597611    
## TreatmentCranberry:LineCR5   0.38990    0.13959   2.793 0.005218 ** 
## TreatmentStrawberry:LineCR5  0.25562    0.18317   1.396 0.162852    
## TreatmentCherry:LineFR1      0.28390    0.07424   3.824 0.000131 ***
## TreatmentCranberry:LineFR1   0.37275    0.07544   4.941 7.78e-07 ***
## TreatmentStrawberry:LineFR1  0.51854    0.07272   7.130 1.00e-12 ***
## TreatmentCherry:LineFR2      0.05431    0.15046   0.361 0.718126    
## TreatmentCranberry:LineFR2   0.52657    0.14966   3.519 0.000434 ***
## TreatmentStrawberry:LineFR2  0.27283    0.15094   1.807 0.070687 .  
## TreatmentCherry:LineFR3      0.05916    0.16402   0.361 0.718350    
## TreatmentCranberry:LineFR3   0.31419    0.16384   1.918 0.055159 .  
## TreatmentStrawberry:LineFR3  0.30784    0.14029   2.194 0.028215 *  
## TreatmentCherry:LineFR4      0.15635    0.08942   1.749 0.080364 .  
## TreatmentCranberry:LineFR4   0.56758    0.09346   6.073 1.25e-09 ***
## TreatmentStrawberry:LineFR4  0.50984    0.09157   5.568 2.58e-08 ***
## TreatmentCherry:LineFR5     -0.04438    0.36064  -0.123 0.902049    
## TreatmentCranberry:LineFR5   0.93843    0.35534   2.641 0.008268 ** 
## TreatmentStrawberry:LineFR5  0.67594    0.25409   2.660 0.007808 ** 
## TreatmentCherry:LineCEA     -0.33378    0.07520  -4.438 9.06e-06 ***
## TreatmentCranberry:LineCEA   0.05785    0.07493   0.772 0.440078    
## TreatmentStrawberry:LineCEA  0.25933    0.07489   3.463 0.000535 ***
## TreatmentCherry:LineCEB     -0.38291    0.07531  -5.085 3.68e-07 ***
## TreatmentCranberry:LineCEB  -0.08653    0.07520  -1.151 0.249844    
## TreatmentStrawberry:LineCEB  0.14071    0.07509   1.874 0.060950 .  
## TreatmentCherry:LineCEC     -0.37932    0.07530  -5.038 4.72e-07 ***
## TreatmentCranberry:LineCEC   0.06855    0.07491   0.915 0.360163    
## TreatmentStrawberry:LineCEC  0.18483    0.07501   2.464 0.013739 *  
## TreatmentCherry:LineCRA     -0.31070    0.07516  -4.134 3.56e-05 ***
## TreatmentCranberry:LineCRA  -0.15067    0.07533  -2.000 0.045469 *  
## TreatmentStrawberry:LineCRA  0.16825    0.07504   2.242 0.024953 *  
## TreatmentCherry:LineCRB     -0.37347    0.07529  -4.961 7.02e-07 ***
## TreatmentCranberry:LineCRB   0.10950    0.07485   1.463 0.143450    
## TreatmentStrawberry:LineCRB  0.19988    0.07499   2.666 0.007687 ** 
## TreatmentCherry:LineCRC     -0.24420    0.07503  -3.255 0.001134 ** 
## TreatmentCranberry:LineCRC  -0.07155    0.07517  -0.952 0.341128    
## TreatmentStrawberry:LineCRC  0.18174    0.07502   2.423 0.015406 *  
## TreatmentCherry:LineCRD     -0.26201    0.07506  -3.491 0.000482 ***
## TreatmentCranberry:LineCRD   0.10853    0.07485   1.450 0.147050    
## TreatmentStrawberry:LineCRD -0.03786    0.07544  -0.502 0.615747    
## TreatmentCherry:LineCRE     -0.33783    0.07521  -4.492 7.06e-06 ***
## TreatmentCranberry:LineCRE   0.01307    0.07501   0.174 0.861633    
## TreatmentStrawberry:LineCRE  0.18226    0.07502   2.430 0.015116 *  
## TreatmentCherry:LineFRA     -0.34947    0.07524  -4.645 3.40e-06 ***
## TreatmentCranberry:LineFRA  -0.06923    0.07516  -0.921 0.357045    
## TreatmentStrawberry:LineFRA  0.12446    0.07512   1.657 0.097540 .  
## TreatmentCherry:LineFRB     -0.49820    0.07557  -6.592 4.33e-11 ***
## TreatmentCranberry:LineFRB  -0.20679    0.07545  -2.741 0.006127 ** 
## TreatmentStrawberry:LineFRB  0.14232    0.07509   1.895 0.058043 .  
## TreatmentCherry:LineFRC     -0.58678    0.07580  -7.742 9.82e-15 ***
## TreatmentCranberry:LineFRC  -0.10565    0.07523  -1.404 0.160247    
## TreatmentStrawberry:LineFRC  0.21520    0.07496   2.871 0.004094 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(8.2114) family taken to be 1)
## 
##     Null deviance: 889732.5  on 1674  degrees of freedom
## Residual deviance:   1730.1  on 1596  degrees of freedom
## AIC: 17660
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  8.211 
##           Std. Err.:  0.301 
## 
##  2 x log-likelihood:  -17501.650
CIfecundity <- confint(mfecundity)

1.2.4 Egg-to-adult viability

## 4 tubes with more adults than eggs
sum(data$Nb_adults>data$Nb_eggs)
## [1] 4
## Number of adults=number of eggs
data$Nb_eggs[data$Nb_adults>data$Nb_eggs] <- data$Nb_adults[data$Nb_adults>data$Nb_eggs]

## Fit model
megg_to_ad <- glm(cbind(Nb_adults, Nb_eggs) ~ -1+Treatment+Line:Treatment, data=data, family="binomial")
summary(megg_to_ad)
## 
## Call:
## glm(formula = cbind(Nb_adults, Nb_eggs) ~ -1 + Treatment + Line:Treatment, 
##     family = "binomial", data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -11.2550   -2.2309    0.0851    2.0910   13.2204  
## 
## Coefficients:
##                              Estimate Std. Error z value Pr(>|z|)    
## TreatmentCherry             -1.788754   0.021611 -82.769  < 2e-16 ***
## TreatmentCranberry          -1.547115   0.021536 -71.838  < 2e-16 ***
## TreatmentStrawberry         -1.326194   0.021024 -63.080  < 2e-16 ***
## TreatmentCherry:LineCE1     -0.494166   0.079164  -6.242 4.31e-10 ***
## TreatmentCranberry:LineCE1  -1.305517   0.114258 -11.426  < 2e-16 ***
## TreatmentStrawberry:LineCE1 -0.387890   0.071590  -5.418 6.02e-08 ***
## TreatmentCherry:LineCE2     -1.115171   0.137737  -8.096 5.66e-16 ***
## TreatmentCranberry:LineCE2  -0.636123   0.133602  -4.761 1.92e-06 ***
## TreatmentStrawberry:LineCE2 -0.046115   0.133611  -0.345 0.729990    
## TreatmentCherry:LineCE3     -0.003858   0.058741  -0.066 0.947636    
## TreatmentCranberry:LineCE3  -0.446447   0.067136  -6.650 2.93e-11 ***
## TreatmentStrawberry:LineCE3 -0.451848   0.060226  -7.503 6.26e-14 ***
## TreatmentCherry:LineCE4     -0.171143   0.092203  -1.856 0.063432 .  
## TreatmentCranberry:LineCE4   0.189949   0.080499   2.360 0.018292 *  
## TreatmentStrawberry:LineCE4 -0.112122   0.080302  -1.396 0.162639    
## TreatmentCherry:LineCR1      0.902703   0.174687   5.168 2.37e-07 ***
## TreatmentCranberry:LineCR1   0.194219   0.098234   1.977 0.048029 *  
## TreatmentStrawberry:LineCR1 -0.699759   0.178558  -3.919 8.89e-05 ***
## TreatmentCherry:LineCR2      0.208505   0.093417   2.232 0.025617 *  
## TreatmentCranberry:LineCR2  -0.354251   0.092819  -3.817 0.000135 ***
## TreatmentStrawberry:LineCR2  0.041436   0.096161   0.431 0.666543    
## TreatmentCherry:LineCR3     -0.910368   0.068209 -13.347  < 2e-16 ***
## TreatmentCranberry:LineCR3  -0.388928   0.049294  -7.890 3.02e-15 ***
## TreatmentStrawberry:LineCR3 -0.234674   0.048389  -4.850 1.24e-06 ***
## TreatmentCherry:LineCR4      0.041249   0.089954   0.459 0.646551    
## TreatmentCranberry:LineCR4  -0.129940   0.076068  -1.708 0.087598 .  
## TreatmentStrawberry:LineCR4  0.205871   0.076808   2.680 0.007355 ** 
## TreatmentCherry:LineCR5      0.446858   0.079930   5.591 2.26e-08 ***
## TreatmentCranberry:LineCR5  -0.201495   0.076001  -2.651 0.008020 ** 
## TreatmentStrawberry:LineCR5 -0.152185   0.100555  -1.513 0.130167    
## TreatmentCherry:LineFR1      0.058675   0.039773   1.475 0.140147    
## TreatmentCranberry:LineFR1  -0.021037   0.039810  -0.528 0.597192    
## TreatmentStrawberry:LineFR1 -0.188028   0.037401  -5.027 4.97e-07 ***
## TreatmentCherry:LineFR2      0.301431   0.078714   3.829 0.000128 ***
## TreatmentCranberry:LineFR2  -0.691834   0.093722  -7.382 1.56e-13 ***
## TreatmentStrawberry:LineFR2 -0.197779   0.083186  -2.378 0.017428 *  
## TreatmentCherry:LineFR3      0.520102   0.078819   6.599 4.15e-11 ***
## TreatmentCranberry:LineFR3  -0.144052   0.090025  -1.600 0.109570    
## TreatmentStrawberry:LineFR3 -0.174482   0.076043  -2.295 0.021761 *  
## TreatmentCherry:LineFR4      0.573374   0.042230  13.577  < 2e-16 ***
## TreatmentCranberry:LineFR4  -0.204735   0.047960  -4.269 1.96e-05 ***
## TreatmentStrawberry:LineFR4  0.070797   0.042863   1.652 0.098590 .  
## TreatmentCherry:LineFR5      0.352270   0.192020   1.835 0.066573 .  
## TreatmentCranberry:LineFR5  -0.538716   0.171142  -3.148 0.001645 ** 
## TreatmentStrawberry:LineFR5 -0.489433   0.131504  -3.722 0.000198 ***
## TreatmentCherry:LineCEA      0.481448   0.043922  10.961  < 2e-16 ***
## TreatmentCranberry:LineCEA  -0.275592   0.048007  -5.741 9.43e-09 ***
## TreatmentStrawberry:LineCEA -0.239612   0.042686  -5.613 1.98e-08 ***
## TreatmentCherry:LineCEB      0.633291   0.042808  14.794  < 2e-16 ***
## TreatmentCranberry:LineCEB  -0.505472   0.055352  -9.132  < 2e-16 ***
## TreatmentStrawberry:LineCEB -0.299214   0.045550  -6.569 5.07e-11 ***
## TreatmentCherry:LineCEC      0.737860   0.041531  17.766  < 2e-16 ***
## TreatmentCranberry:LineCEC  -0.275258   0.047797  -5.759 8.47e-09 ***
## TreatmentStrawberry:LineCEC  0.026482   0.040515   0.654 0.513343    
## TreatmentCherry:LineCRA      0.636074   0.041649  15.272  < 2e-16 ***
## TreatmentCranberry:LineCRA   0.001821   0.047524   0.038 0.969433    
## TreatmentStrawberry:LineCRA -0.465566   0.047651  -9.770  < 2e-16 ***
## TreatmentCherry:LineCRB      0.542770   0.043785  12.396  < 2e-16 ***
## TreatmentCranberry:LineCRB   0.187694   0.040669   4.615 3.93e-06 ***
## TreatmentStrawberry:LineCRB -0.445272   0.046725  -9.530  < 2e-16 ***
## TreatmentCherry:LineCRC      0.328942   0.044466   7.398 1.39e-13 ***
## TreatmentCranberry:LineCRC   0.098592   0.044676   2.207 0.027328 *  
## TreatmentStrawberry:LineCRC -0.471252   0.047486  -9.924  < 2e-16 ***
## TreatmentCherry:LineCRD      0.809372   0.039095  20.703  < 2e-16 ***
## TreatmentCranberry:LineCRD  -0.264808   0.046878  -5.649 1.62e-08 ***
## TreatmentStrawberry:LineCRD -0.987691   0.063323 -15.598  < 2e-16 ***
## TreatmentCherry:LineCRE      0.232873   0.047551   4.897 9.72e-07 ***
## TreatmentCranberry:LineCRE   0.712061   0.036727  19.388  < 2e-16 ***
## TreatmentStrawberry:LineCRE -0.350362   0.045574  -7.688 1.50e-14 ***
## TreatmentCherry:LineFRA     -0.156524   0.054733  -2.860 0.004239 ** 
## TreatmentCranberry:LineFRA  -0.182362   0.048928  -3.727 0.000194 ***
## TreatmentStrawberry:LineFRA  0.032626   0.041352   0.789 0.430124    
## TreatmentCherry:LineFRB      0.691975   0.043965  15.739  < 2e-16 ***
## TreatmentCranberry:LineFRB   0.249832   0.044927   5.561 2.69e-08 ***
## TreatmentStrawberry:LineFRB  0.407777   0.037045  11.008  < 2e-16 ***
## TreatmentCherry:LineFRC      0.908301   0.042838  21.203  < 2e-16 ***
## TreatmentCranberry:LineFRC   0.626745   0.039018  16.063  < 2e-16 ***
## TreatmentStrawberry:LineFRC  0.151208   0.038709   3.906 9.37e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 142163  on 1674  degrees of freedom
## Residual deviance:  17859  on 1596  degrees of freedom
## AIC: 25827
## 
## Number of Fisher Scoring iterations: 5
CImegg_to_ad <- confint(megg_to_ad)

1.3 Build data_logchange dataset

## Extract all CIs
## J'ai garder le nom logchange pour que le code pour la Fig. 4 marche, mais je pense que ça vaudrait le coup que le nom soit plus informatif, comme logfitnesschange par exemple
## Check that the coef are in the same order
identical(names(coef(mfitness)), names(coef(mfecundity)))
## [1] TRUE
identical(names(coef(mfitness)), names(coef(megg_to_ad)))
## [1] TRUE
data_logchange <- data.frame(logchange=coef(mfitness),
                             lowCIlogfitnesschange=CIfitness[, 1],
                             upCIlogfitnesschange=CIfitness[, 2],
                             logfecundchange=coef(mfecundity),
                             lowCIlogfecundchange=CIfecundity[, 1],
                             upCIlogfecundchange=CIfecundity[, 2],
                             logeggtoadchange=coef(megg_to_ad),
                             lowCIlogeggtoadchange=CImegg_to_ad[, 1],
                             upCIlogeggtoadchange=CImegg_to_ad[, 2])
head(data_logchange)
##                               logchange lowCIlogfitnesschange
## TreatmentCherry              3.21847574             3.0875555
## TreatmentCranberry           3.26384919             3.1331965
## TreatmentStrawberry          3.35445512             3.2243029
## TreatmentCherry:LineCE1     -0.27403677            -0.7021017
## TreatmentCranberry:LineCE1  -0.91247393            -1.4069620
## TreatmentStrawberry:LineCE1  0.09553243            -0.3637574
##                             upCIlogfitnesschange logfecundchange
## TreatmentCherry                        3.3543047       5.0072295
## TreatmentCranberry                     3.3994333       4.8109641
## TreatmentStrawberry                    3.4895811       4.6806486
## TreatmentCherry:LineCE1                0.1969999       0.2174413
## TreatmentCranberry:LineCE1            -0.3690326       0.3930426
## TreatmentStrawberry:LineCE1            0.6127366       0.4834228
##                             lowCIlogfecundchange upCIlogfecundchange
## TreatmentCherry                      4.937707676           5.0782276
## TreatmentCranberry                   4.741037413           4.8823484
## TreatmentStrawberry                  4.610407277           4.7523331
## TreatmentCherry:LineCE1             -0.008054728           0.4563722
## TreatmentCranberry:LineCE1           0.144087552           0.6591387
## TreatmentStrawberry:LineCE1          0.234153348           0.7497917
##                             logeggtoadchange lowCIlogeggtoadchange
## TreatmentCherry                   -1.7887538            -1.8313270
## TreatmentCranberry                -1.5471149            -1.5895200
## TreatmentStrawberry               -1.3261935            -1.3675662
## TreatmentCherry:LineCE1           -0.4941655            -0.6522000
## TreatmentCranberry:LineCE1        -1.3055165            -1.5366802
## TreatmentStrawberry:LineCE1       -0.3878904            -0.5301533
##                             upCIlogeggtoadchange
## TreatmentCherry                       -1.7466080
## TreatmentCranberry                    -1.5050959
## TreatmentStrawberry                   -1.2851499
## TreatmentCherry:LineCE1               -0.3417120
## TreatmentCranberry:LineCE1            -1.0882241
## TreatmentStrawberry:LineCE1           -0.2494137
## Remove estimates from ancestral population
data_logchange <- data_logchange[4:nrow(data_logchange), ]
  
## Extract information regarding line, selective and test media
rowname <- strsplit(rownames(data_logchange), split=":")
Treatment <- as.factor(gsub("Treatment", "", lapply(rowname, `[[`, 1)))
Generation <- Line <- as.factor(gsub("Line", "", lapply(rowname, `[[`, 2)))
## Change Fruit_s levels
Fruit_s <- as.factor(substr(Line, 1, 2))
levels(Fruit_s) <- levels(Treatment)
## Change levels
levels(Generation) <- ifelse(is.na(as.numeric(substr(levels(Generation), 3, 3))), "29", "7")

## Combine data
data_info <- data.frame(Treatment, Line, Fruit_s, Generation)
data_info$Line_Treatement <- paste(data_info$Line, data_info$Treatment, sep="_")
# Add symp and allop
data_info$SA <- as.factor(ifelse(data_info$Treatment == data_info$Fruit_s , 1, 0))
data.frame(names(coef(mfitness))[4:length(coef(mfitness))], data_info)
##    names.coef.mfitness...4.length.coef.mfitness...  Treatment Line    Fruit_s
## 1                          TreatmentCherry:LineCE1     Cherry  CE1     Cherry
## 2                       TreatmentCranberry:LineCE1  Cranberry  CE1     Cherry
## 3                      TreatmentStrawberry:LineCE1 Strawberry  CE1     Cherry
## 4                          TreatmentCherry:LineCE2     Cherry  CE2     Cherry
## 5                       TreatmentCranberry:LineCE2  Cranberry  CE2     Cherry
## 6                      TreatmentStrawberry:LineCE2 Strawberry  CE2     Cherry
## 7                          TreatmentCherry:LineCE3     Cherry  CE3     Cherry
## 8                       TreatmentCranberry:LineCE3  Cranberry  CE3     Cherry
## 9                      TreatmentStrawberry:LineCE3 Strawberry  CE3     Cherry
## 10                         TreatmentCherry:LineCE4     Cherry  CE4     Cherry
## 11                      TreatmentCranberry:LineCE4  Cranberry  CE4     Cherry
## 12                     TreatmentStrawberry:LineCE4 Strawberry  CE4     Cherry
## 13                         TreatmentCherry:LineCR1     Cherry  CR1  Cranberry
## 14                      TreatmentCranberry:LineCR1  Cranberry  CR1  Cranberry
## 15                     TreatmentStrawberry:LineCR1 Strawberry  CR1  Cranberry
## 16                         TreatmentCherry:LineCR2     Cherry  CR2  Cranberry
## 17                      TreatmentCranberry:LineCR2  Cranberry  CR2  Cranberry
## 18                     TreatmentStrawberry:LineCR2 Strawberry  CR2  Cranberry
## 19                         TreatmentCherry:LineCR3     Cherry  CR3  Cranberry
## 20                      TreatmentCranberry:LineCR3  Cranberry  CR3  Cranberry
## 21                     TreatmentStrawberry:LineCR3 Strawberry  CR3  Cranberry
## 22                         TreatmentCherry:LineCR4     Cherry  CR4  Cranberry
## 23                      TreatmentCranberry:LineCR4  Cranberry  CR4  Cranberry
## 24                     TreatmentStrawberry:LineCR4 Strawberry  CR4  Cranberry
## 25                         TreatmentCherry:LineCR5     Cherry  CR5  Cranberry
## 26                      TreatmentCranberry:LineCR5  Cranberry  CR5  Cranberry
## 27                     TreatmentStrawberry:LineCR5 Strawberry  CR5  Cranberry
## 28                         TreatmentCherry:LineFR1     Cherry  FR1 Strawberry
## 29                      TreatmentCranberry:LineFR1  Cranberry  FR1 Strawberry
## 30                     TreatmentStrawberry:LineFR1 Strawberry  FR1 Strawberry
## 31                         TreatmentCherry:LineFR2     Cherry  FR2 Strawberry
## 32                      TreatmentCranberry:LineFR2  Cranberry  FR2 Strawberry
## 33                     TreatmentStrawberry:LineFR2 Strawberry  FR2 Strawberry
## 34                         TreatmentCherry:LineFR3     Cherry  FR3 Strawberry
## 35                      TreatmentCranberry:LineFR3  Cranberry  FR3 Strawberry
## 36                     TreatmentStrawberry:LineFR3 Strawberry  FR3 Strawberry
## 37                         TreatmentCherry:LineFR4     Cherry  FR4 Strawberry
## 38                      TreatmentCranberry:LineFR4  Cranberry  FR4 Strawberry
## 39                     TreatmentStrawberry:LineFR4 Strawberry  FR4 Strawberry
## 40                         TreatmentCherry:LineFR5     Cherry  FR5 Strawberry
## 41                      TreatmentCranberry:LineFR5  Cranberry  FR5 Strawberry
## 42                     TreatmentStrawberry:LineFR5 Strawberry  FR5 Strawberry
## 43                         TreatmentCherry:LineCEA     Cherry  CEA     Cherry
## 44                      TreatmentCranberry:LineCEA  Cranberry  CEA     Cherry
## 45                     TreatmentStrawberry:LineCEA Strawberry  CEA     Cherry
## 46                         TreatmentCherry:LineCEB     Cherry  CEB     Cherry
## 47                      TreatmentCranberry:LineCEB  Cranberry  CEB     Cherry
## 48                     TreatmentStrawberry:LineCEB Strawberry  CEB     Cherry
## 49                         TreatmentCherry:LineCEC     Cherry  CEC     Cherry
## 50                      TreatmentCranberry:LineCEC  Cranberry  CEC     Cherry
## 51                     TreatmentStrawberry:LineCEC Strawberry  CEC     Cherry
## 52                         TreatmentCherry:LineCRA     Cherry  CRA  Cranberry
## 53                      TreatmentCranberry:LineCRA  Cranberry  CRA  Cranberry
## 54                     TreatmentStrawberry:LineCRA Strawberry  CRA  Cranberry
## 55                         TreatmentCherry:LineCRB     Cherry  CRB  Cranberry
## 56                      TreatmentCranberry:LineCRB  Cranberry  CRB  Cranberry
## 57                     TreatmentStrawberry:LineCRB Strawberry  CRB  Cranberry
## 58                         TreatmentCherry:LineCRC     Cherry  CRC  Cranberry
## 59                      TreatmentCranberry:LineCRC  Cranberry  CRC  Cranberry
## 60                     TreatmentStrawberry:LineCRC Strawberry  CRC  Cranberry
## 61                         TreatmentCherry:LineCRD     Cherry  CRD  Cranberry
## 62                      TreatmentCranberry:LineCRD  Cranberry  CRD  Cranberry
## 63                     TreatmentStrawberry:LineCRD Strawberry  CRD  Cranberry
## 64                         TreatmentCherry:LineCRE     Cherry  CRE  Cranberry
## 65                      TreatmentCranberry:LineCRE  Cranberry  CRE  Cranberry
## 66                     TreatmentStrawberry:LineCRE Strawberry  CRE  Cranberry
## 67                         TreatmentCherry:LineFRA     Cherry  FRA Strawberry
## 68                      TreatmentCranberry:LineFRA  Cranberry  FRA Strawberry
## 69                     TreatmentStrawberry:LineFRA Strawberry  FRA Strawberry
## 70                         TreatmentCherry:LineFRB     Cherry  FRB Strawberry
## 71                      TreatmentCranberry:LineFRB  Cranberry  FRB Strawberry
## 72                     TreatmentStrawberry:LineFRB Strawberry  FRB Strawberry
## 73                         TreatmentCherry:LineFRC     Cherry  FRC Strawberry
## 74                      TreatmentCranberry:LineFRC  Cranberry  FRC Strawberry
## 75                     TreatmentStrawberry:LineFRC Strawberry  FRC Strawberry
##    Generation Line_Treatement SA
## 1           7      CE1_Cherry  1
## 2           7   CE1_Cranberry  0
## 3           7  CE1_Strawberry  0
## 4           7      CE2_Cherry  1
## 5           7   CE2_Cranberry  0
## 6           7  CE2_Strawberry  0
## 7           7      CE3_Cherry  1
## 8           7   CE3_Cranberry  0
## 9           7  CE3_Strawberry  0
## 10          7      CE4_Cherry  1
## 11          7   CE4_Cranberry  0
## 12          7  CE4_Strawberry  0
## 13          7      CR1_Cherry  0
## 14          7   CR1_Cranberry  1
## 15          7  CR1_Strawberry  0
## 16          7      CR2_Cherry  0
## 17          7   CR2_Cranberry  1
## 18          7  CR2_Strawberry  0
## 19          7      CR3_Cherry  0
## 20          7   CR3_Cranberry  1
## 21          7  CR3_Strawberry  0
## 22          7      CR4_Cherry  0
## 23          7   CR4_Cranberry  1
## 24          7  CR4_Strawberry  0
## 25          7      CR5_Cherry  0
## 26          7   CR5_Cranberry  1
## 27          7  CR5_Strawberry  0
## 28          7      FR1_Cherry  0
## 29          7   FR1_Cranberry  0
## 30          7  FR1_Strawberry  1
## 31          7      FR2_Cherry  0
## 32          7   FR2_Cranberry  0
## 33          7  FR2_Strawberry  1
## 34          7      FR3_Cherry  0
## 35          7   FR3_Cranberry  0
## 36          7  FR3_Strawberry  1
## 37          7      FR4_Cherry  0
## 38          7   FR4_Cranberry  0
## 39          7  FR4_Strawberry  1
## 40          7      FR5_Cherry  0
## 41          7   FR5_Cranberry  0
## 42          7  FR5_Strawberry  1
## 43         29      CEA_Cherry  1
## 44         29   CEA_Cranberry  0
## 45         29  CEA_Strawberry  0
## 46         29      CEB_Cherry  1
## 47         29   CEB_Cranberry  0
## 48         29  CEB_Strawberry  0
## 49         29      CEC_Cherry  1
## 50         29   CEC_Cranberry  0
## 51         29  CEC_Strawberry  0
## 52         29      CRA_Cherry  0
## 53         29   CRA_Cranberry  1
## 54         29  CRA_Strawberry  0
## 55         29      CRB_Cherry  0
## 56         29   CRB_Cranberry  1
## 57         29  CRB_Strawberry  0
## 58         29      CRC_Cherry  0
## 59         29   CRC_Cranberry  1
## 60         29  CRC_Strawberry  0
## 61         29      CRD_Cherry  0
## 62         29   CRD_Cranberry  1
## 63         29  CRD_Strawberry  0
## 64         29      CRE_Cherry  0
## 65         29   CRE_Cranberry  1
## 66         29  CRE_Strawberry  0
## 67         29      FRA_Cherry  0
## 68         29   FRA_Cranberry  0
## 69         29  FRA_Strawberry  1
## 70         29      FRB_Cherry  0
## 71         29   FRB_Cranberry  0
## 72         29  FRB_Strawberry  1
## 73         29      FRC_Cherry  0
## 74         29   FRC_Cranberry  0
## 75         29  FRC_Strawberry  1
## Compute sample size per line and test medium
sample_size <- aggregate(Nb_eggs~Line:Treatment, length, data=data[data$Generation!="0",])
sample_size$Line_Treatement <- paste(sample_size$Line, sample_size$Treatment, sep="_")
names(sample_size)[3] <- "N"

## Merge the three datasets
data_info <- merge(x=data_info, y=sample_size[, 3:4], by="Line_Treatement")
data_info$Line_Treatment <- paste0("Treatment", data_info$Treatment, ":Line", data_info$Line)
data_logchange$Line_Treatment <- rownames(data_logchange)
data_logchange <- merge(x=data_info, y=data_logchange, by = "Line_Treatment")[, -c(1, 2)]

head(data_logchange)
##   Treatment Line Fruit_s Generation SA  N  logchange lowCIlogfitnesschange
## 1    Cherry  CE1  Cherry          7  1 10 -0.2740368           -0.70210174
## 2    Cherry  CE2  Cherry          7  1  4 -0.5617188           -1.21534852
## 3    Cherry  CE3  Cherry          7  1 13  0.1852825           -0.18979611
## 4    Cherry  CE4  Cherry          7  1  5  0.1279134           -0.43781493
## 5    Cherry  CEA  Cherry         29  1 30  0.1476700           -0.12317382
## 6    Cherry  CEB  Cherry         29  1 30  0.2503803           -0.01964191
##   upCIlogfitnesschange logfecundchange lowCIlogfecundchange upCIlogfecundchange
## 1            0.1969999       0.2174413         -0.008054728           0.4563722
## 2            0.2019585       0.5534521          0.216090324           0.9271423
## 3            0.5938166       0.1891403         -0.012370462           0.4006261
## 4            0.7888315       0.2990560         -0.007390043           0.6339803
## 5            0.4301216      -0.3337778         -0.479544492          -0.1846294
## 6            0.5320919      -0.3829105         -0.528887054          -0.2335638
##   logeggtoadchange lowCIlogeggtoadchange upCIlogeggtoadchange
## 1     -0.494165542            -0.6522000         -0.341712049
## 2     -1.115170967            -1.3960146         -0.855075837
## 3     -0.003857865            -0.1201706          0.110148034
## 4     -0.171142635            -0.3556475          0.006047628
## 5      0.481447806             0.3949970          0.567185941
## 6      0.633290781             0.5490961          0.716914363

1.4 Build sympatric/allopatric datasets

#Formatting for testing correlation
TEMP_dataG7_CheCran <- formattinglogchange(logchange_dataset = data_logchange, generation="7", 
                                           fruitcomb=c("Cherry", "Cranberry"), trait="fitness")
TEMP_dataG7_CranStraw <- formattinglogchange(logchange_dataset = data_logchange, generation="7", 
                                             fruitcomb=c("Strawberry", "Cranberry"), trait="fitness")
TEMP_dataG7_StrawChe <- formattinglogchange(logchange_dataset = data_logchange, generation="7", 
                                            fruitcomb=c("Cherry", "Strawberry"), trait="fitness")

TEMP_dataG29_CheCran <- formattinglogchange(logchange_dataset = data_logchange, generation="29", 
                                            fruitcomb=c("Cherry", "Cranberry"), trait="fitness")
TEMP_dataG29_CranStraw <- formattinglogchange(logchange_dataset = data_logchange, generation="29", 
                                              fruitcomb=c("Strawberry", "Cranberry"), trait="fitness")
TEMP_dataG29_StrawChe <- formattinglogchange(logchange_dataset = data_logchange, generation="29", 
                                             fruitcomb=c("Cherry", "Strawberry"), trait="fitness")

2 ADAPTATION

2.1 Proportion of adaptation: raw data

tapply(data_sum$Nb_adults[data_sum$Generation=="2"],data_sum$Fruit_s[data_sum$Generation=="2"],mean)
##     Cherry  Cranberry Strawberry 
##    8.80000   19.38667   12.19000
tapply(data_sum$Nb_adults[data_sum$Generation=="27"],data_sum$Fruit_s[data_sum$Generation=="27"],mean)
##     Cherry  Cranberry Strawberry 
##   35.84000   33.63750   42.33333
## Calcul proportion change between G2 and G27 
((tapply(data_sum$Nb_adults[data_sum$Generation=="27"],data_sum$Fruit_s[data_sum$Generation=="27"],mean) -
    tapply(data_sum$Nb_adults[data_sum$Generation=="2"],data_sum$Fruit_s[data_sum$Generation=="2"],mean)) / 
  tapply(data_sum$Nb_adults[data_sum$Generation=="2"],data_sum$Fruit_s[data_sum$Generation=="2"],mean)) * 100
##     Cherry  Cranberry Strawberry 
##  307.27273   73.50843  247.27919
# 
# (abs(tapply(data_sum$fitness[data_sum$Generation=="27"],data_sum$Fruit_s[data_sum$Generation=="27"],mean) -
#     tapply(data_sum$fitness[data_sum$Generation=="2"],data_sum$Fruit_s[data_sum$Generation=="2"],mean)) / 
#   abs(tapply(data_sum$fitness[data_sum$Generation=="2"],data_sum$Fruit_s[data_sum$Generation=="2"],mean))) * 100


## Calcul proportion change between G2 and phase2 (because G8 is not representative)  
((tapply(data_sum$Nb_adults[data_sum$Generation=="8"],data_sum$Fruit_s[data_sum$Generation=="8"],mean) -
    tapply(data_sum$Nb_adults[data_sum$Generation=="2"],data_sum$Fruit_s[data_sum$Generation=="2"],mean)) / 
  tapply(data_sum$Nb_adults[data_sum$Generation=="2"],data_sum$Fruit_s[data_sum$Generation=="2"],mean)) * 100
##     Cherry  Cranberry Strawberry 
##  289.61039   85.02183   89.19658
## Compute proportion change between G2 and phase 2 (because G8 is not representative)  
((tapply(data_sum$Nb_adults[data_sum$Phase=="pool"],data_sum$Fruit_s[data_sum$Phase=="pool"],mean) -
    tapply(data_sum$Nb_adults[data_sum$Phase=="first_prepool"],data_sum$Fruit_s[data_sum$Phase=="first_prepool"],mean)) / 
  tapply(data_sum$Nb_adults[data_sum$Phase=="first_prepool"],data_sum$Fruit_s[data_sum$Phase=="first_prepool"],mean)) * 100
##     Cherry  Cranberry Strawberry 
##  123.10237   93.62985   45.43089
((tapply(data_sum$Nb_adults[data_sum$Phase=="second_postpool"],data_sum$Fruit_s[data_sum$Phase=="second_postpool"],mean) -
    tapply(data_sum$Nb_adults[data_sum$Phase=="first_prepool"],data_sum$Fruit_s[data_sum$Phase=="first_prepool"],mean)) / 
  tapply(data_sum$Nb_adults[data_sum$Phase=="first_prepool"],data_sum$Fruit_s[data_sum$Phase=="first_prepool"],mean)) * 100
##     Cherry  Cranberry Strawberry 
##   230.1300   176.0760   145.1609

2.2 Analysis

######## Models
mod1 <- lme4::lmer(fitness ~ 1 + (1|Generation:Fruit_s), 
            weights = N, data = data_sum, REML = FALSE) 
mod2 <- lme4::lmer(fitness~ Phase +  (1|Generation:Fruit_s), 
            weights = N, data = data_sum, REML = FALSE)
mod3 <- lme4::lmer(fitness ~ Generation +(1|Generation:Fruit_s), 
            weights = N, data = data_sum, REML = FALSE) 
mod4 <- lme4::lmer(fitness ~ Fruit_s + (1|Generation:Fruit_s), 
            weights = N, data = data_sum, REML = FALSE) 
mod5 <- lme4::lmer(fitness ~ Fruit_s*Phase +  (1|Generation:Fruit_s), 
            weights = N, data = data_sum, REML = FALSE) 
mod6 <- lme4::lmer(fitness ~ Fruit_s*Generation + (1|Generation:Fruit_s), 
            weights = N, data = data_sum, REML = FALSE) 
mod7 <- lme4::lmer(fitness ~ Fruit_s + Phase + (1|Generation:Fruit_s), 
            weights = N, data = data_sum, REML = FALSE) 
mod8 <- lme4::lmer(fitness ~ Fruit_s + Generation +  (1|Generation:Fruit_s), 
            weights = N, data = data_sum, REML = FALSE) 

MuMIn::model.sel(mod1, mod2, mod3, mod4, mod5, mod6, mod7, mod8)
## Model selection table 
##        (Int) Phs     Gnr Frt_s Frt_s:Phs Frt_s:Gnr             family df
## mod7 -0.3308   +             +                     gaussian(identity)  7
## mod2 -0.4763   +                                   gaussian(identity)  5
## mod5 -0.5220   +             +         +           gaussian(identity) 11
## mod8 -0.1484     0.03835     +                     gaussian(identity)  6
## mod3 -0.2975     0.03872                           gaussian(identity)  4
## mod6 -0.3025     0.04821     +                   + gaussian(identity)  8
## mod1  0.2953                                       gaussian(identity)  3
## mod4  0.4452                 +                     gaussian(identity)  5
##        logLik  AICc delta weight
## mod7 -122.683 259.8  0.00  0.811
## mod2 -126.513 263.3  3.44  0.145
## mod5 -121.280 265.7  5.85  0.044
## mod8 -137.588 287.5 27.69  0.000
## mod3 -140.167 288.5 28.67  0.000
## mod6 -136.994 290.6 30.76  0.000
## mod1 -154.997 316.1 56.26  0.000
## mod4 -153.115 316.5 56.64  0.000
## Models ranked by AICc(x) 
## Random terms (all models): 
## '1 | Generation:Fruit_s'
summary(mod7)
## Linear mixed model fit by maximum likelihood  ['lmerMod']
## Formula: fitness ~ Fruit_s + Phase + (1 | Generation:Fruit_s)
##    Data: data_sum
## Weights: N
## 
##      AIC      BIC   logLik deviance df.resid 
##    259.4    283.9   -122.7    245.4      240 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -5.2489 -0.3779  0.0806  0.4943  2.2620 
## 
## Random effects:
##  Groups             Name        Variance Std.Dev.
##  Generation:Fruit_s (Intercept) 0.07089  0.2663  
##  Residual                       2.11535  1.4544  
## Number of obs: 247, groups:  Generation:Fruit_s, 72
## 
## Fixed effects:
##                      Estimate Std. Error t value
## (Intercept)          -0.33076    0.11201  -2.953
## Fruit_sCranberry     -0.16227    0.09128  -1.778
## Fruit_sStrawberry    -0.26301    0.09342  -2.816
## Phasepool             0.57610    0.13295   4.333
## Phasesecond_postpool  0.99771    0.10710   9.316
## 
## Correlation of Fixed Effects:
##             (Intr) Frt_sC Frt_sS Phaspl
## Frt_sCrnbrr -0.437                     
## Frt_sStrwbr -0.433  0.528              
## Phasepool   -0.619 -0.027 -0.026       
## Phsscnd_pst -0.791  0.003  0.011  0.663
mod_Phase <- lme4::lmer(fitness ~ Fruit_s + Phase + (1|Generation:Fruit_s), 
            weights = N, data = data_sum) 


#Posthoc
emmeans::emmeans(mod_Phase, list(pairwise ~ Phase), adjust = "tukey") #
## $`emmeans of Phase`
##  Phase           emmean     SE     df lower.CL upper.CL
##  first_prepool   -0.473 0.1012    728   -0.715   -0.230
##  pool             0.105 0.0942 101168   -0.120    0.330
##  second_postpool  0.525 0.0469   2673    0.413    0.637
## 
## Results are averaged over the levels of: Fruit_s 
## Degrees-of-freedom method: kenward-roger 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 3 estimates 
## 
## $`pairwise differences of Phase`
##  contrast                        estimate    SE    df t.ratio p.value
##  first_prepool - pool              -0.577 0.138  2390 -4.177  0.0001 
##  first_prepool - second_postpool   -0.997 0.112   866 -8.942  <.0001 
##  pool - second_postpool            -0.420 0.105 29408 -3.993  0.0002 
## 
## Results are averaged over the levels of: Fruit_s 
## Degrees-of-freedom method: kenward-roger 
## P value adjustment: tukey method for comparing a family of 3 estimates
emmeans::emmeans(mod_Phase, list(pairwise ~ Phase+Fruit_s), adjust = "tukey") #
## $`emmeans of Phase, Fruit_s`
##  Phase           Fruit_s     emmean     SE    df lower.CL upper.CL
##  first_prepool   Cherry     -0.3307 0.1165   960  -0.6537 -0.00774
##  pool            Cherry      0.2466 0.1125 21049  -0.0645  0.55772
##  second_postpool Cherry      0.6666 0.0736  4340   0.4629  0.87027
##  first_prepool   Cranberry  -0.4933 0.1139   881  -0.8090 -0.17751
##  pool            Cranberry   0.0840 0.1067 23944  -0.2110  0.37913
##  second_postpool Cranberry   0.5040 0.0701  1279   0.3098  0.69830
##  first_prepool   Strawberry -0.5936 0.1151   937  -0.9127 -0.27462
##  pool            Strawberry -0.0163 0.1080 29146  -0.3149  0.28223
##  second_postpool Strawberry  0.4037 0.0729  4392   0.2018  0.60549
## 
## Degrees-of-freedom method: kenward-roger 
## Confidence level used: 0.95 
## Conf-level adjustment: sidak method for 9 estimates 
## 
## $`pairwise differences of Phase, Fruit_s`
##  contrast                                               estimate     SE    df
##  first_prepool Cherry - pool Cherry                       -0.577 0.1382  2390
##  first_prepool Cherry - second_postpool Cherry            -0.997 0.1115   866
##  first_prepool Cherry - first_prepool Cranberry            0.163 0.0950  2321
##  first_prepool Cherry - pool Cranberry                    -0.415 0.1657  2334
##  first_prepool Cherry - second_postpool Cranberry         -0.835 0.1469  1044
##  first_prepool Cherry - first_prepool Strawberry           0.263 0.0969  4121
##  first_prepool Cherry - pool Strawberry                   -0.314 0.1668  2829
##  first_prepool Cherry - second_postpool Strawberry        -0.734 0.1486  1498
##  pool Cherry - second_postpool Cherry                     -0.420 0.1052 29408
##  pool Cherry - first_prepool Cranberry                     0.740 0.1697  2401
##  pool Cherry - pool Cranberry                              0.163 0.0950  2321
##  pool Cherry - second_postpool Cranberry                  -0.257 0.1445  4431
##  pool Cherry - first_prepool Strawberry                    0.840 0.1708  2822
##  pool Cherry - pool Strawberry                             0.263 0.0969  4121
##  pool Cherry - second_postpool Strawberry                 -0.157 0.1462  8921
##  second_postpool Cherry - first_prepool Cranberry          1.160 0.1462  1503
##  second_postpool Cherry - pool Cranberry                   0.583 0.1390 11020
##  second_postpool Cherry - second_postpool Cranberry        0.163 0.0950  2321
##  second_postpool Cherry - first_prepool Strawberry         1.260 0.1470  1475
##  second_postpool Cherry - pool Strawberry                  0.683 0.1399 10394
##  second_postpool Cherry - second_postpool Strawberry       0.263 0.0969  4121
##  first_prepool Cranberry - pool Cranberry                 -0.577 0.1382  2390
##  first_prepool Cranberry - second_postpool Cranberry      -0.997 0.1115   866
##  first_prepool Cranberry - first_prepool Strawberry        0.100 0.0935  2401
##  first_prepool Cranberry - pool Strawberry                -0.477 0.1668  2420
##  first_prepool Cranberry - second_postpool Strawberry     -0.897 0.1460  1523
##  pool Cranberry - second_postpool Cranberry               -0.420 0.1052 29408
##  pool Cranberry - first_prepool Strawberry                 0.678 0.1669  2367
##  pool Cranberry - pool Strawberry                          0.100 0.0935  2401
##  pool Cranberry - second_postpool Strawberry              -0.320 0.1412 10759
##  second_postpool Cranberry - first_prepool Strawberry      1.098 0.1451  1033
##  second_postpool Cranberry - pool Strawberry               0.520 0.1403  4684
##  second_postpool Cranberry - second_postpool Strawberry    0.100 0.0935  2401
##  first_prepool Strawberry - pool Strawberry               -0.577 0.1382  2390
##  first_prepool Strawberry - second_postpool Strawberry    -0.997 0.1115   866
##  pool Strawberry - second_postpool Strawberry             -0.420 0.1052 29408
##  t.ratio p.value
##  -4.177  0.0010 
##  -8.942  <.0001 
##   1.711  0.7402 
##  -2.502  0.2311 
##  -5.683  <.0001 
##   2.712  0.1436 
##  -1.884  0.6247 
##  -4.943  <.0001 
##  -3.993  0.0021 
##   4.360  0.0005 
##   1.711  0.7402 
##  -1.782  0.6942 
##   4.920  <.0001 
##   2.712  0.1436 
##  -1.074  0.9779 
##   7.935  <.0001 
##   4.190  0.0009 
##   1.711  0.7402 
##   8.574  <.0001 
##   4.883  <.0001 
##   2.712  0.1436 
##  -4.177  0.0010 
##  -8.942  <.0001 
##   1.074  0.9779 
##  -2.858  0.0995 
##  -6.144  <.0001 
##  -3.993  0.0021 
##   4.061  0.0017 
##   1.074  0.9779 
##  -2.264  0.3646 
##   7.566  <.0001 
##   3.710  0.0065 
##   1.074  0.9779 
##  -4.177  0.0010 
##  -8.942  <.0001 
##  -3.993  0.0021 
## 
## Degrees-of-freedom method: kenward-roger 
## P value adjustment: tukey method for comparing a family of 9 estimates

2.3 Plot

#Position dodge
pd <- ggplot2::position_dodge(0.3) # move them .05 to the left and right

#Extract slope and intercept
dat_predict_allfruits <- expand.grid(Generation=as.numeric(levels(as.factor(data_sum$Generation))),
                                     Fruit_s=unique(data_sum$Fruit_s))

dat_predict_allfruits$Phase <- ifelse(dat_predict_allfruits$Generation<= 7, "first_prepool",
                                     ifelse(dat_predict_allfruits$Generation>=12, "second_postpool", "pool"))
dat_predict_allfruits$fitness_predicted <- predict(mod_Phase, newdata = dat_predict_allfruits, 
                          re.form= NA, type = "response")

#REAL DATA
#Add G1 G6 and G7
TEMP_lineG1 <- c(rep(NA, 2), 1,rep(NA, 5))
TEMP_lineG6 <- c(rep(NA, 2), 6,rep(NA, 5))
TEMP_lineG7 <- c(rep(NA, 2), 7,rep(NA, 5))

TEMP_total <- rbind(data_sum,TEMP_lineG1,TEMP_lineG1,TEMP_lineG1,TEMP_lineG6,TEMP_lineG6,TEMP_lineG6,TEMP_lineG7,TEMP_lineG7,TEMP_lineG7)
TEMP_total$Fruit_s[TEMP_total$Generation == "6"] <- c("Strawberry", "Cranberry", "Cherry")
TEMP_total$Fruit_s[TEMP_total$Generation == "1"] <- c("Strawberry", "Cranberry", "Cherry")
TEMP_total$Fruit_s[TEMP_total$Generation == "7"] <- c("Strawberry", "Cranberry", "Cherry")
tail(TEMP_total)
##     Line    Fruit_s Generation Phase  N Nb_adults sd fitness se_fitness
## 251 <NA> Strawberry          6  <NA> NA        NA NA      NA         NA
## 252 <NA>  Cranberry          6  <NA> NA        NA NA      NA         NA
## 253 <NA>     Cherry          6  <NA> NA        NA NA      NA         NA
## 254 <NA> Strawberry          7  <NA> NA        NA NA      NA         NA
## 255 <NA>  Cranberry          7  <NA> NA        NA NA      NA         NA
## 256 <NA>     Cherry          7  <NA> NA        NA NA      NA         NA
## Add label 
TEMP_anno <- data.frame(x1 = c(3.5, 3.5,  10, 3.5,  3.5, 10,  3.5, 3.5, 10), 
                        x2 = c(9, 17.5, 17.5,9, 17.5,  17.5,9,17.5, 17.5),
                        y1 = c(1,2,1.2,1,2,1.2,1,2,1.2), 
                        y2 = c(1.25,  2.25, 1.45, 1.25,  2.25, 1.45,1.25,  2.25, 1.45),
                        xstar = c(6.5,10,14,6.5,10,14,6.5,10,14), 
                        ystar = c(1.5,2.5,1.7,1.5,2.5,1.7,1.5,2.5,1.7),
                        lab = c("**", "***", "**", "**",  "***",  "**", "**" ,"***",  "**"),
                        Fruit_s = c("Cherry", "Cherry", "Cherry",
                               "Cranberry", "Cranberry", "Cranberry",
                               "Strawberry","Strawberry","Strawberry"), 
                        Line = NA)
TEMP_anno
##     x1   x2  y1   y2 xstar ystar lab    Fruit_s Line
## 1  3.5  9.0 1.0 1.25   6.5   1.5  **     Cherry   NA
## 2  3.5 17.5 2.0 2.25  10.0   2.5 ***     Cherry   NA
## 3 10.0 17.5 1.2 1.45  14.0   1.7  **     Cherry   NA
## 4  3.5  9.0 1.0 1.25   6.5   1.5  **  Cranberry   NA
## 5  3.5 17.5 2.0 2.25  10.0   2.5 ***  Cranberry   NA
## 6 10.0 17.5 1.2 1.45  14.0   1.7  **  Cranberry   NA
## 7  3.5  9.0 1.0 1.25   6.5   1.5  ** Strawberry   NA
## 8  3.5 17.5 2.0 2.25  10.0   2.5 *** Strawberry   NA
## 9 10.0 17.5 1.2 1.45  14.0   1.7  ** Strawberry   NA
TEMP_title <- data.frame(xtitle = c(14, 14, 14),
                       ytitle = c(3, 3, 3),
                       title = c("Cherry", "Cranberry", "Strawberry"),
                       Fruit_s = c("Cherry", "Cranberry", "Strawberry"), 
                       Line = NA)
TEMP_title
##   xtitle ytitle      title    Fruit_s Line
## 1     14      3     Cherry     Cherry   NA
## 2     14      3  Cranberry  Cranberry   NA
## 3     14      3 Strawberry Strawberry   NA
PLOT_FITNESS_CHERRY <- ggplot(data = TEMP_total[TEMP_total$Fruit_s == "Cherry",], 
                            aes(x = factor(Generation),group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin =fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.2,color = "black") + 
  geom_line(size = 0.3,position = pd) + 
  geom_line(data = dat_predict_allfruits[dat_predict_allfruits$Fruit_s == "Cherry",], 
                     aes(x = factor(Generation), y = fitness_predicted,
                         colour = "black", group = Phase), size = 0.5) + 
  geom_point(size =1, position = pd, shape =21, fill = "white") + 
  ylim(-3, 3.05) + 
  ylab("Fitness") + 
  xlab("Generation") + 
  geom_text(data = TEMP_anno[TEMP_anno$Fruit_s == "Cherry",], aes(x = xstar,  y = ystar, label = lab), size =3.3) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Cherry",], aes(x = x1, xend = x1, 
           y = y1, yend = y2), size = 0.4) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Cherry",], aes(x = x2, xend = x2, 
           y = y1, yend = y2), size = 0.4) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Cherry",], aes(x = x1, xend = x2, 
           y = y2, yend = y2), size = 0.4) + 
  scale_color_manual(values = c("black", "#BC3C6D", "#FDB424", "#3FAA96")) + 
  ggtitle("Cherry") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#BC3C6D"))
PLOT_FITNESS_CHERRY

PLOT_FITNESS_CRANB <- ggplot2::ggplot(data = TEMP_total[TEMP_total$Fruit_s == "Cranberry",], 
                            aes(x = factor(Generation),group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin =fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.2,color = "black") + 
  geom_line(size = 0.3,position = pd) + 
  geom_line(data = dat_predict_allfruits[dat_predict_allfruits$Fruit_s == "Cranberry",], aes(x = factor(Generation), y = fitness_predicted, colour = "black", group = Phase),
                size = 0.5) + 
  geom_point(size =1, position = pd, shape =21, fill = "white") + 
  ylim(-3, 3.05) + 
  ylab("Fitness") + 
  xlab("Generation") + 
  geom_text(data = TEMP_anno[TEMP_anno$Fruit_s == "Cranberry",], aes(x = xstar,  y = ystar, label = lab), size =3.3) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Cranberry",], aes(x = x1, xend = x1, 
           y = y1, yend = y2), size = 0.4) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Cranberry",], aes(x = x2, xend = x2, 
           y = y1, yend = y2), size = 0.4) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Cranberry",], aes(x = x1, xend = x2, 
           y = y2, yend = y2), size = 0.4) + 
  scale_color_manual(values = c("black", "#FDB424")) + 
  ggtitle("Cranberry") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#FDB424"))
PLOT_FITNESS_CRANB

PLOT_FITNESS_STRAW <- ggplot2::ggplot(data = TEMP_total[TEMP_total$Fruit_s == "Strawberry",], 
                            aes(x = factor(Generation),group = Line, y = fitness, colour =Fruit_s)) + 
  geom_errorbar(aes(ymin =fitness-1.96*se_fitness, ymax = fitness + 1.96*se_fitness),
                width=.1,position = pd, size = 0.2,color = "black") + 
  geom_line(size = 0.3,position = pd) + 
  geom_line(data = dat_predict_allfruits[dat_predict_allfruits$Fruit_s == "Strawberry",], 
            aes(x = factor(Generation), y = fitness_predicted, colour = "black", group = Phase),
            size = 0.5) + 
  geom_point(size =1, position = pd, shape =21, fill = "white") + 
  ylim(-3, 3.05) + 
  ylab("Fitness") + 
  xlab("Generation") + 
  geom_text(data = TEMP_anno[TEMP_anno$Fruit_s == "Strawberry",], 
            aes(x = xstar,  y = ystar, label = lab), size =3.3) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Strawberry",], 
               aes(x = x1, xend = x1, y = y1, yend = y2), size = 0.4) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Strawberry",], 
               aes(x = x2, xend = x2, y = y1, yend = y2), size = 0.4) + 
  geom_segment(data = TEMP_anno[TEMP_anno$Fruit_s == "Strawberry",], 
               aes(x = x1, xend = x2, y = y2, yend = y2), size = 0.4) + 
  scale_color_manual(values = c("black", "#3FAA96")) + 
  ggtitle("Strawberry") + 
  theme_LO_adaptation + theme(plot.title = element_text(color = "#3FAA96"))
PLOT_FITNESS_STRAW

DYNAMIQUE_THREE_JOIN <- cowplot::ggdraw() + 
  cowplot::draw_plot(PLOT_FITNESS_CHERRY + theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.line.x = element_blank()), 
            x = 0, y = 0.66, width = 1, height = 0.33) + 
  cowplot::draw_plot(PLOT_FITNESS_CRANB + theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.line.x = element_blank()), 
            x = 0, y = 0.33, width = 1, height = 0.33) + 
  cowplot::draw_plot(PLOT_FITNESS_STRAW, 
            x = 0, y = 00, width = 1, height = 0.33) + 
  cowplot::draw_plot_label(c("A", "B", "C"),  
                  x = c(0, 0, 0), 
                  y = c(1, 0.66, 0.33), 
                  hjust = c(-0.5, -0.5, -0.5), 
                  vjust = c(1.5, 1.5, 1.5),
                  size = 12) 
 DYNAMIQUE_THREE_JOIN

cowplot::save_plot(file =here::here("figures", "FIG_Adaptation.pdf"), DYNAMIQUE_THREE_JOIN, base_height = 17/cm(1), base_width = 11/cm(1), dpi = 1200)

3 HETEROGENEITY

3.1 Plot fitness

pd <-  position_dodge(width = 0.5)

################### INTERMEDIATE PHENOTYPING

# Re-order levels of Line
data_sum_G7 <- data_logchange[data_logchange$Generation == "7",]
data_sum_G7$Line <- factor(data_sum_G7$Line, levels= c("CE2", "CE1", "CE4", "CE3",
                                                       "CR2", "CR3", "CR5", "CR1", "CR4",
                                                       "FR2", "FR3", "FR5", "FR1", "FR4"))


#Plot: 
symp_allop_g7 <- ggplot(data = data_sum_G7, 
                      aes(x = Line, y = logchange, group = Treatment, 
                          color = Fruit_s, shape = Treatment, fill = Fruit_s)) + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "grey", size = 0.5) + 
  geom_errorbar(aes(ymin = lowCIlogfitnesschange, 
                    ymax = upCIlogfitnesschange),
                  width = 0.2, size = 0.5, alpha = 0.6,position = pd) + 
  geom_point(position = pd, fill = "white", size =3) + 
  ylab("Fitness change between intermediate\nand initial phenotyping steps")  + 
  xlab("Populations") + 
  labs(shape = "Test fruit", color = "Selection fruit") + 
  guides(color = FALSE,
           shape = guide_legend(override.aes = list(fill = c("black")))) + 
  scale_shape_manual(values = c(21, 22, 24)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  scale_fill_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  theme_LO_sober + theme(axis.text.x  = element_blank()) +
  coord_cartesian(expand = FALSE, ylim = c(-2.7, 2.7), xlim=c(0, 15),clip = "off") + 
  ggtitle("Intermediate phenotyping step") + 
  annotate("text", x = 2.5, y = 2.4, label = 'bold(" Evolved\non cherry")',
                     size =3,colour = "#BC3C6D",parse = TRUE) + 
  annotate("text", x = 7.5, y = 2.4, label = 'bold("   Evolved\non cranberry")',
                     size =3,colour = "#FDB424",parse = TRUE) + 
  annotate("text", x = 12.5, y = 2.4, label = 'bold("   Evolved\non strawberry")',
                     size =3,colour = "#3FAA96",parse = TRUE) + 
  geom_segment(x = 15.5, y = -0.1, xend= 15.5, yend = -1.6, size = 0.15, 
               arrow = arrow(length = unit(0.05, "npc")),colour = "black") + 
  geom_segment(x = 15.5, y = 0.1, xend= 15.5, yend = 1.6, size = 0.15,
               arrow = arrow(length = unit(0.05, "npc")),colour = "black") + 
  annotate("text", x = 16.2, y = 0.7, label = 'bold(" Fitness\nincrease")',
             size = 3,colour = "black", parse = TRUE, angle =90) + 
  annotate("text", x = 16.2, y =-0.7, label = 'bold("  Fitness\ndecrease")',
              size = 3,colour = "black", parse = TRUE, angle =90) 
symp_allop_g7

################### FINAL PHENOTYPING

# Re-order levels of Line
data_sum_G29 <- data_logchange[data_logchange$Generation == "29",]
data_sum_G29$Line <- factor(data_sum_G29$Line, levels = c("CEA", "CEB", "CEC", 
                                                          "CRD", "CRA", "CRC", "CRB", "CRE",
                                                          "FRA", "FRC", "FRB"))

symp_allop_G29 <- ggplot(data = data_sum_G29, 
              aes(x = Line, y=logchange, group = Treatment, 
                  color =Fruit_s, shape = Treatment,fill =Fruit_s)) + 
      geom_hline(yintercept = 0, linetype = "dashed", color = "grey", size = 0.5) + 
    geom_point(size =3, position = pd) + 
    geom_errorbar(aes(ymin =  lowCIlogfitnesschange,
                      ymax = upCIlogfitnesschange),
                width= 0.2, size = 0.5, alpha = 0.6,position = pd) + 
    ylab("Fitness change between final\nand initial phenotyping steps")  + 
    xlab("Populations") + 
    labs(shape = "Test fruit", color = "Selection fruit") + 
    guides(fill = FALSE, alpha = FALSE) + 
    scale_shape_manual(values = c(21, 22, 24)) + 
    scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
    scale_fill_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
    theme_LO_sober + theme(axis.text.x  = element_blank()) +
    coord_cartesian(expand = FALSE, ylim = c(-2, 2), xlim=c(0, 11),clip = "off") + 
  annotate("text", x = 2, y =1.5, label = 'bold(" Evolved\non cherry")',
                     size =3,colour = "#BC3C6D",parse = TRUE) + 
  annotate("text", x = 6, y =1.5, label = 'bold("   Evolved\non cranberry")',
                     size =3,colour = "#FDB424",parse = TRUE) + 
  annotate("text", x = 10, y =1.5, label = 'bold("   Evolved\non strawberry")',
                     size =3,colour = "#3FAA96",parse = TRUE) + 
  geom_segment(x = 11.5, y = -0.1, xend= 11.5, yend = -1.2, size = 0.15, 
               arrow = arrow(length = unit(0.05, "npc")),colour = "black") + 
    geom_segment(x = 11.5, y = 0.1, xend= 11.5, yend = 1.2, size = 0.15,
               arrow = arrow(length = unit(0.05, "npc")),colour = "black") + 
    annotate("text", x = 12, y = 0.5, label = 'bold(" Fitness\nincrease")',
             size =3,colour = "black",parse = TRUE, angle =90) + 
    annotate("text", x = 12, y = -0.5, label = 'bold("  Fitness\ndecrease")',
              size =3,colour = "black", parse = TRUE, angle =90) + 
  ggtitle("Final phenotyping step") 
symp_allop_G29

### Add stroke
DIF_FITNESS_G7 <- symp_allop_g7 + 
  geom_point(aes(alpha = SA, color = interaction(SA, Fruit_s)), 
             position = pd, size =3, stroke = 1.5, fill = "white") + 
  scale_alpha_manual(values = c(0, 1)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96", 
                                "#7C2748", "#CA8702", "#328677", 
                                "#BC3C6D", "#FDB424", "#3FAA96"))  
DIF_FITNESS_G7

DIF_FITNESS_G29 <- symp_allop_G29 + geom_point(aes(alpha = SA, color = interaction(SA,Fruit_s)), position = pd, size =3, stroke =1.5) + 
      scale_alpha_manual(values = c(0, 1)) + 
      scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96", "#7C2748", "#CA8702", "#328677", "#BC3C6D", "#FDB424", "#3FAA96"))  
DIF_FITNESS_G29

plot_legend <- ggplot(data = data_logchange, aes(x = Line, y=logchange, group = Treatment, 
                  color =Fruit_s, shape = Treatment,fill =Fruit_s)) + 
    geom_point(size =3, position = pd) + 
    labs(shape = "Test fruit", color = "Selection fruit") + 
    scale_shape_manual(values = c(21, 22, 24)) + 
    scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
    theme_LO_sober + 
    guides(fill = FALSE) + 
    theme(legend.key.size = unit(0.5, "cm"),
        axis.text.x  = element_blank()) +
    coord_cartesian(expand = FALSE, ylim = c(-2, 2), xlim=c(0, 11),clip = "off") 


###########################################################
#############################################ALL PLOT
legend_trade <- lemon::g_legend(plot_legend)
    

SYMP_ALLOP_TOTAL <- cowplot::ggdraw() + 
  cowplot::draw_plot(DIF_FITNESS_G7 + theme(legend.position = 'none', 
                             plot.margin =unit(c(0.5, 3.5, 0.5, 0.5), "cm")),
            x = 0, y = 0.5, width = 1, height = 0.5) + 
  cowplot::draw_plot(DIF_FITNESS_G29 + theme(legend.position = 'none', 
                             plot.margin =unit(c(0.5, 3.5, 0.5, 0.5), "cm")), 
            x = 0, y = 0, width = 1, height = 0.5) + 
  cowplot::draw_plot_label(c("A", "B"),  
                  x = c(0, 0), y = c(1, 0.5), 
                  size = 14) + 
  cowplot::draw_plot(legend_trade, x = 0.93, y = 0.5, width = 0.001, height = 0.001) 

SYMP_ALLOP_TOTAL

cowplot::save_plot(file =here::here("figures", "FIG_Heterogeneity.pdf"),  
                   SYMP_ALLOP_TOTAL, base_height = 20/cm(1), base_width = 25/cm(1), dpi = 1200)

3.2 Plot fecundity

pd <-  position_dodge(width = 0.5)

################### INTERMEDIATE PHENOTYPING
symp_allop_g7_FECUNDITY <- ggplot(data = data_sum_G7, 
                      aes(x = Line, y = logfecundchange, group = Treatment, 
                          color = Fruit_s, shape = Treatment, fill = Fruit_s)) + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "grey", size = 0.5) + 
  geom_errorbar(aes(ymin = lowCIlogfecundchange, 
                    ymax = upCIlogfecundchange, linetype = "dashed"),
                  width = 0.2, size = 0.5, alpha = 0.6, position = pd) + 
  geom_point(position = pd, fill = "white", size =3) + 
  ylab("Change in log fecundity between\nintermediate and initial phenotyping steps")  + 
  xlab("Populations") + 
  labs(shape = "Test fruit", color = "Selection fruit") + 
  guides(color = FALSE,
           shape = guide_legend(override.aes = list(fill = c("black")))) + 
  scale_shape_manual(values = c(21, 22, 24)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  scale_fill_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  theme_LO_sober + theme(axis.text.x  = element_blank()) +
  coord_cartesian(expand = FALSE, ylim = c(-1.1, 2), xlim=c(0, 15),clip = "off") + 
  ggtitle("Intermediate phenotyping step")  +
  annotate("text", x = 2.5, y = 1.8, label = 'bold(" Evolved\non cherry")',
                     size =3,colour = "#BC3C6D",parse = TRUE) + 
  annotate("text", x = 7.5, y = 1.8, label = 'bold("   Evolved\non cranberry")',
                     size =3,colour = "#FDB424",parse = TRUE) + 
  annotate("text", x = 12.5, y = 1.8, label = 'bold("   Evolved\non strawberry")',
                     size =3,colour = "#3FAA96",parse = TRUE)
symp_allop_g7_FECUNDITY

################### FINAL PHENOTYPING
symp_allop_G29_FECUNDITY <- ggplot(data = data_sum_G29, 
              aes(x = Line, y=logfecundchange, group = Treatment, 
                  color =Fruit_s, shape = Treatment,fill =Fruit_s)) + 
      geom_hline(yintercept = 0, linetype = "dashed", color = "grey", size = 0.5) + 
    geom_point(size =3, position = pd) + 
    geom_errorbar(aes(ymin =lowCIlogfecundchange, ymax =upCIlogfecundchange),
                width= 0.2, size = 0.5, alpha = 0.6,position = pd) + 
    ylab("Change in log fecundity between\nfinal and initial phenotyping steps")  + 
    xlab("Populations") + 
    labs(shape = "Test fruit", color = "Selection fruit") + 
    guides(fill = FALSE, alpha = FALSE) + 
    scale_shape_manual(values = c(21, 22, 24)) + 
    scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
    scale_fill_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
    theme_LO_sober + theme(axis.text.x  = element_blank()) +
  coord_cartesian(expand = FALSE, ylim = c(-1.1, 2), xlim=c(0, 11),clip = "off") + 
  annotate("text", x = 2, y = 1.4, label = 'bold(" Evolved\non cherry")',
                     size =3,colour = "#BC3C6D",parse = TRUE) + 
  annotate("text", x = 6, y = 1.4, label = 'bold("   Evolved\non cranberry")',
                     size =3,colour = "#FDB424",parse = TRUE) + 
  annotate("text", x = 10, y = 1.4, label = 'bold("   Evolved\non strawberry")',
                     size =3,colour = "#3FAA96",parse = TRUE) +
  ggtitle("Final phenotyping step") 
symp_allop_G29_FECUNDITY

### Add stroke
DIF_FECUNDITY_G7 <- symp_allop_g7_FECUNDITY + 
  geom_point(aes(alpha = SA, color = interaction(SA, Fruit_s)), position = pd, size =3, stroke =1.5, fill = "white") + 
  scale_alpha_manual(values = c(0, 1)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96", "#7C2748", "#CA8702", "#328677", "#BC3C6D", "#FDB424", "#3FAA96"))  
DIF_FECUNDITY_G7

DIF_FECUNDITY_G29 <- symp_allop_G29_FECUNDITY + 
  geom_point(aes(alpha = SA, color = interaction(SA,Fruit_s)), 
             position = pd, size =3, stroke =1.5) + 
      scale_alpha_manual(values = c(0, 1)) + 
      scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96", "#7C2748", "#CA8702", "#328677", "#BC3C6D", "#FDB424", "#3FAA96"))  
DIF_FECUNDITY_G29

plot_legend <- ggplot(data = data_logchange, aes(x = Line, y=logchange, group = Treatment, 
                  color =Fruit_s, shape = Treatment,fill =Fruit_s)) + 
    geom_point(size =3, position = pd) + 
    labs(shape = "Test fruit", color = "Selection fruit") + 
    scale_shape_manual(values = c(21, 22, 24)) + 
    scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
    theme_LO_sober + 
    guides(fill = FALSE) + 
    theme(legend.key.size = unit(0.5, "cm"),
        axis.text.x  = element_blank()) +
    coord_cartesian(expand = FALSE, ylim = c(-2, 2), xlim=c(0, 11),clip = "off") 


###########################################################
#############################################ALL PLOT
legend_trade <- lemon::g_legend(plot_legend)
    

SYMP_ALLOP_TOTAL_FEC <- cowplot::ggdraw() + 
  cowplot::draw_plot(DIF_FECUNDITY_G7 + theme(legend.position = 'none', 
                             plot.margin =unit(c(0.5, 3.5, 0.5, 0.5), "cm")),
            x = 0, y = 0.5, width = 1, height = 0.5) + 
  cowplot::draw_plot(DIF_FECUNDITY_G29 + theme(legend.position = 'none', 
                             plot.margin =unit(c(0.5, 3.5, 0.5, 0.5), "cm")), 
            x = 0, y = 0, width = 1, height = 0.5) + 
  cowplot::draw_plot_label(c("A", "B"),  
                  x = c(0, 0), y = c(1, 0.5), 
                  size = 14) + 
  cowplot::draw_plot(legend_trade, x = 0.93, y = 0.5, width = 0.001, height = 0.001) 
# 
# cowplot::save_plot(file =here::here("figures", "FIG_SX_HeterogeneityNb_eggs.pdf"),  
#                    SYMP_ALLOP_TOTAL_FEC, base_height = 20/cm(1), base_width = 25/cm(1), dpi = 1200)

3.3 Plot egg-to-adult viability

pd <-  position_dodge(width = 0.5)



################### INTERMEDIATE PHENOTYPING
symp_allop_g7_VIABILITIY <- ggplot(data = data_sum_G7, 
                      aes(x = Line, y = logeggtoadchange, group = Treatment, 
                          color = Fruit_s, shape = Treatment, fill = Fruit_s)) + 
  geom_hline(yintercept = 0, linetype = "dashed", color = "grey", size = 0.5) + 
  geom_errorbar(aes(ymin = lowCIlogeggtoadchange, 
                    ymax =upCIlogeggtoadchange, linetype = "dashed"),
                  width = 0.2, size = 0.5, alpha = 0.6,position = pd) + 
  geom_point(position = pd, fill = "white", size =3) + 
  ylab("Change in logit egg-to-adult viability\nbetween intermediate and initial phenotyping steps")  + 
  xlab("Populations") + 
  labs(shape = "Test fruit", color = "Selection fruit") + 
  guides(color = FALSE,
           shape = guide_legend(override.aes = list(fill = c("black")))) + 
  scale_shape_manual(values = c(21, 22, 24)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  scale_fill_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
  theme_LO_sober + theme(axis.text.x  = element_blank()) +
  coord_cartesian(expand = FALSE, ylim = c(-1.7, 1.6), xlim=c(0, 15),clip = "off") + 
  ggtitle("Intermediate phenotyping step") + 
  annotate("text", x = 2.5, y = 1.4, label = 'bold(" Evolved\non cherry")',
                     size =3,colour = "#BC3C6D",parse = TRUE) + 
  annotate("text", x = 7.5, y = 1.4, label = 'bold("   Evolved\non cranberry")',
                     size =3,colour = "#FDB424",parse = TRUE) + 
  annotate("text", x = 12.5, y = 1.4, label = 'bold("   Evolved\non strawberry")',
                     size =3,colour = "#3FAA96",parse = TRUE)
symp_allop_g7_VIABILITIY

################### FINAL PHENOTYPING
symp_allop_G29_VIABILITIY <- ggplot(data = data_sum_G29, 
              aes(x = Line, y=logeggtoadchange, group = Treatment, 
                  color =Fruit_s, shape = Treatment,fill =Fruit_s)) + 
      geom_hline(yintercept = 0, linetype = "dashed", color = "grey", size = 0.5) + 
    geom_point(size =3, position = pd) + 
    geom_errorbar(aes(ymin =lowCIlogeggtoadchange, ymax =upCIlogeggtoadchange),
                width= 0.2, size = 0.5, alpha = 0.6,position = pd) + 
    ylab("Change in logit egg-to-adult viability\nbetween final and initial phenotyping steps")  + 
    xlab("Populations") + 
    labs(shape = "Test fruit", color = "Selection fruit") + 
    guides(fill = FALSE, alpha = FALSE) + 
    scale_shape_manual(values = c(21, 22, 24)) + 
    scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
    scale_fill_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
    theme_LO_sober + theme(axis.text.x  = element_blank()) +
    coord_cartesian(expand = FALSE, ylim = c(-1.7, 1.6), xlim=c(0, 11),clip = "off") + 
  annotate("text", x = 2, y = 1.4, label = 'bold(" Evolved\non cherry")',
                     size =3,colour = "#BC3C6D",parse = TRUE) + 
  annotate("text", x = 6, y = 1.4, label = 'bold("   Evolved\non cranberry")',
                     size =3,colour = "#FDB424",parse = TRUE) + 
  annotate("text", x = 10, y = 1.4, label = 'bold("   Evolved\non strawberry")',
                     size =3,colour = "#3FAA96",parse = TRUE) + 
  ggtitle("Final phenotyping step") 
symp_allop_G29_VIABILITIY

### Add stroke
DIF_VIABILITY_G7 <- symp_allop_g7_VIABILITIY + 
  geom_point(aes(alpha = SA, color = interaction(SA,Fruit_s)), position = pd, size =3, stroke =1.5, fill = "white") + 
  scale_alpha_manual(values = c(0, 1)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96", "#7C2748", "#CA8702", "#328677", "#BC3C6D", "#FDB424", "#3FAA96"))  
DIF_VIABILITY_G7

DIF_VIABILITY_G29 <- symp_allop_G29_VIABILITIY + 
  geom_point(aes(alpha = SA, color = interaction(SA,Fruit_s)), 
             position = pd, size =3, stroke =1.5) + 
      scale_alpha_manual(values = c(0, 1)) + 
      scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96", "#7C2748", "#CA8702", "#328677", "#BC3C6D", "#FDB424", "#3FAA96"))  
DIF_VIABILITY_G29

plot_legend <- ggplot(data = data_logchange, aes(x = Line, y=logchange, group = Treatment, 
                  color =Fruit_s, shape = Treatment,fill =Fruit_s)) + 
    geom_point(size =3, position = pd) + 
    labs(shape = "Test fruit", color = "Selection fruit") + 
    scale_shape_manual(values = c(21, 22, 24)) + 
    scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96")) + 
    theme_LO_sober + 
    guides(fill = FALSE) + 
    theme(legend.key.size = unit(0.5, "cm"),
        axis.text.x  = element_blank()) +
    coord_cartesian(expand = FALSE, ylim = c(-2, 2), xlim=c(0, 11),clip = "off") 


###########################################################
#############################################ALL PLOT
legend_trade <- lemon::g_legend(plot_legend)
    

SYMP_ALLOP_TOTAL_VIABILITY <- cowplot::ggdraw() + 
  cowplot::draw_plot(DIF_VIABILITY_G7 + theme(legend.position = 'none', 
                             plot.margin =unit(c(0.5, 3.5, 0.5, 0.5), "cm")),
            x = 0, y = 0.5, width = 1, height = 0.5) + 
  cowplot::draw_plot(DIF_VIABILITY_G29 + theme(legend.position = 'none', 
                             plot.margin =unit(c(0.5, 3.5, 0.5, 0.5), "cm")), 
            x = 0, y = 0, width = 1, height = 0.5) + 
  cowplot::draw_plot_label(c("A", "B"),  
                  x = c(0, 0), y = c(1, 0.5), 
                  size = 14) + 
  cowplot::draw_plot(legend_trade, x = 0.93, y = 0.5, width = 0.001, height = 0.001) 

SYMP_ALLOP_TOTAL_VIABILITY

# cowplot::save_plot(file =here::here("figures", "FIG_SX_HeterogeneityEgg-to-adultviability.pdf"),  
#                    SYMP_ALLOP_TOTAL_VIABILITY, base_height = 20/cm(1), base_width = 25/cm(1), dpi = 1200)


#################### ALL SUPPLEMENTS: Fecundity and Viability
DIF_SYMP_ALLOP_TOTAL_SUPPLEMENTS <- cowplot::ggdraw() + 
  cowplot::draw_plot(DIF_FECUNDITY_G7 + theme(legend.position = 'none', 
                             plot.margin =unit(c(0.5, 3.5, 0.5, 0.5), "cm"), 
                             axis.title.x=element_text(size = 12), 
                             axis.title.y=element_text(size = 12)),
            x = 0, y = 0.45, width = 0.55, height = 0.5) + 
  cowplot::draw_plot(DIF_FECUNDITY_G29 + theme(legend.position = 'none', 
                             plot.margin =unit(c(0.5, 3.5, 0.5, 0.5), "cm"), 
                             axis.title.x=element_text(size = 12), 
                             axis.title.y=element_text(size = 12)), 
            x = 0, y = 0, width = 0.55, height = 0.5) + 
  cowplot::draw_plot(DIF_VIABILITY_G7 + theme(legend.position = 'none', 
                             plot.margin =unit(c(0.5, 3.5, 0.5, 0.5),"cm"), 
                             axis.title.x=element_text(size = 12), 
                             axis.title.y=element_text(size = 12)),
            x = 0.45, y = 0.45, width = 0.55, height = 0.5) + 
  cowplot::draw_plot(DIF_VIABILITY_G29 + theme(legend.position = 'none', 
                             plot.margin =unit(c(0.5, 3.5, 0.5, 0.5), "cm"), 
                             axis.title.x=element_text(size = 12), 
                             axis.title.y=element_text(size = 12)),
            x = 0.45, y = 0, width = 0.55, height = 0.5) + 
  cowplot::draw_plot_label(c("A", "Fecundity", "B", "Egg-to-adult viability"),  
                  x = c(0, 0.15, 0.45, 0.53), y = c(1, 1, 1, 1), 
                  size = c(17, 15, 17, 15)) + 
  cowplot::draw_plot(legend_trade, x = 0.94, y = 0.5, width = 0.001, height = 0.001) 
DIF_SYMP_ALLOP_TOTAL_SUPPLEMENTS

cowplot::save_plot(file =here::here("figures", "FIG_SX_Heterogeneity_Fecundity_Viability.pdf"),  
                   DIF_SYMP_ALLOP_TOTAL_SUPPLEMENTS, 
                   base_height = 20/cm(1), base_width = 30/cm(1), dpi = 1200)

3.4 Analysis fitness

3.4.1 Compare fit of Poisson lognormal and negative binomial distributions

## Poisson lognormal model
mpoislognormal <- lme4::glmer(Nb_adults ~ Generation*Fruit_s*Treatment + (1|Obs), family="poisson", data=data)
summary(mpoislognormal)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: poisson  ( log )
## Formula: Nb_adults ~ Generation * Fruit_s * Treatment + (1 | Obs)
##    Data: data
## 
##      AIC      BIC   logLik deviance df.resid 
##  14644.6  14763.9  -7300.3  14600.6     1652 
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -1.98315 -0.16385  0.04689  0.14592  0.34552 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Obs    (Intercept) 0.5636   0.7507  
## Number of obs: 1674, groups:  Obs, 1674
## 
## Fixed effects:
##                                                    Estimate Std. Error z value
## (Intercept)                                         3.04363    0.07866  38.693
## Generation29                                        0.17011    0.11412   1.491
## Generation7                                        -0.08779    0.16053  -0.547
## Fruit_sCranberry                                   -0.04282    0.19499  -0.220
## Fruit_sStrawberry                                   0.58170    0.17148   3.392
## TreatmentCranberry                                  0.07202    0.11097   0.649
## TreatmentStrawberry                                 0.16668    0.11082   1.504
## Generation29:Fruit_sCranberry                       0.06431    0.22120   0.291
## Generation29:Fruit_sStrawberry                     -0.77126    0.20775  -3.712
## Generation29:TreatmentCranberry                    -0.55720    0.16196  -3.440
## Generation7:TreatmentCranberry                     -0.15416    0.23335  -0.661
## Generation29:TreatmentStrawberry                   -0.22375    0.16108  -1.389
## Generation7:TreatmentStrawberry                     0.29309    0.23243   1.261
## Fruit_sCranberry:TreatmentCranberry                 0.36292    0.27293   1.330
## Fruit_sStrawberry:TreatmentCranberry               -0.12228    0.24988  -0.489
## Fruit_sCranberry:TreatmentStrawberry                0.04192    0.27801   0.151
## Fruit_sStrawberry:TreatmentStrawberry              -0.41137    0.24640  -1.669
## Generation29:Fruit_sCranberry:TreatmentCranberry    0.05299    0.31080   0.171
## Generation29:Fruit_sStrawberry:TreatmentCranberry   0.64959    0.30038   2.163
## Generation29:Fruit_sCranberry:TreatmentStrawberry  -0.56909    0.31524  -1.805
## Generation29:Fruit_sStrawberry:TreatmentStrawberry  0.91491    0.29665   3.084
##                                                    Pr(>|z|)    
## (Intercept)                                         < 2e-16 ***
## Generation29                                       0.136051    
## Generation7                                        0.584439    
## Fruit_sCranberry                                   0.826176    
## Fruit_sStrawberry                                  0.000693 ***
## TreatmentCranberry                                 0.516306    
## TreatmentStrawberry                                0.132574    
## Generation29:Fruit_sCranberry                      0.771276    
## Generation29:Fruit_sStrawberry                     0.000205 ***
## Generation29:TreatmentCranberry                    0.000581 ***
## Generation7:TreatmentCranberry                     0.508838    
## Generation29:TreatmentStrawberry                   0.164825    
## Generation7:TreatmentStrawberry                    0.207314    
## Fruit_sCranberry:TreatmentCranberry                0.183617    
## Fruit_sStrawberry:TreatmentCranberry               0.624596    
## Fruit_sCranberry:TreatmentStrawberry               0.880156    
## Fruit_sStrawberry:TreatmentStrawberry              0.095022 .  
## Generation29:Fruit_sCranberry:TreatmentCranberry   0.864610    
## Generation29:Fruit_sStrawberry:TreatmentCranberry  0.030575 *  
## Generation29:Fruit_sCranberry:TreatmentStrawberry  0.071035 .  
## Generation29:Fruit_sStrawberry:TreatmentStrawberry 0.002041 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## fit warnings:
## fixed-effect model matrix is rank deficient so dropping 15 columns / coefficients
## convergence code: 0
## Model failed to converge with max|grad| = 0.0517486 (tol = 0.002, component 1)
## Check convergence
derivs1 <- mpoislognormal@optinfo$derivs
sc_grad1 <- with(derivs1,solve(Hessian,gradient))
max(abs(sc_grad1))
## [1] 0.005763628
## Negative binomial model
mnegbin <- MASS::glm.nb(Nb_adults ~ Generation*Fruit_s*Treatment, data=data)

## Compare AIC
AIC(mnegbin, mpoislognormal)
##                df      AIC
## mnegbin        22 14360.58
## mpoislognormal 22 14644.57
## Simulate data with Poisson lognormal distribution
x.teo.poislognormal <- unlist(simulate(mpoislognormal))
x.teo.negbin <- unlist(simulate(mnegbin))


## QQplot to compared Negative binomial and Poisson log normal distributions
qqplot(data$Nb_adults, x.teo.negbin, main="QQ-plot", xlab="Observed data", ylab="Simulated data", las=1, xlim = c(0, 100), ylim = c(0, 100)) ## QQ-plot
points(sort(data$Nb_adults), sort(x.teo.poislognormal), pch=1, col='red')
abline(0,1)
## Add legend
legend(0, 70, legend=c("Neg. Bin.", "Pois. Lognormal"), col=c("black", "red"), pch=1, bty="n")

## Negative binomial distribution provides a better fit to the data

3.4.2 Compare average fitess changes

m0 <- MASS::glm.nb(Nb_adults ~ Generation*Fruit_s, data=data)
summary(m0)
## 
## Call:
## MASS::glm.nb(formula = Nb_adults ~ Generation * Fruit_s, data = data, 
##     init.theta = 1.968231636, link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.4615  -0.7782  -0.0830   0.4461   2.8089  
## 
## Coefficients: (5 not defined because of singularities)
##                                  Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                     3.2805352  0.0426489  76.920  < 2e-16 ***
## Generation29                    0.0136542  0.0619522   0.220   0.8256    
## Generation7                     0.0007216  0.0899488   0.008   0.9936    
## Fruit_sCranberry                0.0951520  0.1052162   0.904   0.3658    
## Fruit_sGF                              NA         NA      NA       NA    
## Fruit_sStrawberry               0.3909076  0.0957667   4.082 4.47e-05 ***
## Generation29:Fruit_sCranberry  -0.0627878  0.1195761  -0.525   0.5995    
## Generation7:Fruit_sCranberry           NA         NA      NA       NA    
## Generation29:Fruit_sGF                 NA         NA      NA       NA    
## Generation7:Fruit_sGF                  NA         NA      NA       NA    
## Generation29:Fruit_sStrawberry -0.1903868  0.1148241  -1.658   0.0973 .  
## Generation7:Fruit_sStrawberry          NA         NA      NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(1.9682) family taken to be 1)
## 
##     Null deviance: 1941.9  on 1673  degrees of freedom
## Residual deviance: 1890.4  on 1667  degrees of freedom
## AIC: 14428
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  1.9682 
##           Std. Err.:  0.0724 
## 
##  2 x log-likelihood:  -14412.2880
## G7: fitness increase for strawberry populations, fitness increased for cherry and cranberry populations
## G29: fitness increase for strawberry populations, no fitness change for cherry and cranberry populations

m1 <- MASS::glm.nb(Nb_adults ~ -1 + Generation_Fruit_s_Treatment, data=data)
summary(m1)
## 
## Call:
## MASS::glm.nb(formula = Nb_adults ~ -1 + Generation_Fruit_s_Treatment, 
##     data = data, init.theta = 2.092227879, link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.5666  -0.7954  -0.0810   0.4562   3.7626  
## 
## Coefficients:
##                                                      Estimate Std. Error
## Generation_Fruit_s_Treatment0_GF_Cherry               3.21848    0.07197
## Generation_Fruit_s_Treatment0_GF_Cranberry            3.26385    0.07185
## Generation_Fruit_s_Treatment0_GF_Strawberry           3.35446    0.07162
## Generation_Fruit_s_Treatment29_Cherry_Cherry          3.47438    0.07520
## Generation_Fruit_s_Treatment29_Cherry_Cranberry       2.94034    0.07680
## Generation_Fruit_s_Treatment29_Cherry_Strawberry      3.39002    0.07540
## Generation_Fruit_s_Treatment29_Cranberry_Cherry       3.44744    0.05830
## Generation_Fruit_s_Treatment29_Cranberry_Cranberry    3.47176    0.05825
## Generation_Fruit_s_Treatment29_Cranberry_Strawberry   2.99139    0.05934
## Generation_Fruit_s_Treatment29_Strawberry_Cherry      3.28175    0.07568
## Generation_Fruit_s_Treatment29_Strawberry_Cranberry   3.41955    0.07533
## Generation_Fruit_s_Treatment29_Strawberry_Strawberry  3.72917    0.07468
## Generation_Fruit_s_Treatment7_Cherry_Cherry           3.19356    0.12735
## Generation_Fruit_s_Treatment7_Cherry_Cranberry        3.12613    0.13652
## Generation_Fruit_s_Treatment7_Cherry_Strawberry       3.50322    0.13718
## Generation_Fruit_s_Treatment7_Cranberry_Cherry        3.17683    0.12363
## Generation_Fruit_s_Treatment7_Cranberry_Cranberry     3.38900    0.10784
## Generation_Fruit_s_Treatment7_Cranberry_Strawberry    3.52468    0.12041
## Generation_Fruit_s_Treatment7_Strawberry_Cherry       3.71796    0.09074
## Generation_Fruit_s_Treatment7_Strawberry_Cranberry    3.56222    0.09343
## Generation_Fruit_s_Treatment7_Strawberry_Strawberry   3.71993    0.08790
##                                                      z value Pr(>|z|)    
## Generation_Fruit_s_Treatment0_GF_Cherry                44.72   <2e-16 ***
## Generation_Fruit_s_Treatment0_GF_Cranberry             45.43   <2e-16 ***
## Generation_Fruit_s_Treatment0_GF_Strawberry            46.84   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Cherry           46.20   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Cranberry        38.29   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Strawberry       44.96   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Cherry        59.14   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Cranberry     59.60   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Strawberry    50.41   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Cherry       43.36   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Cranberry    45.40   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Strawberry   49.93   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Cherry            25.08   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Cranberry         22.90   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Strawberry        25.54   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Cherry         25.70   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Cranberry      31.43   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Strawberry     29.27   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Cherry        40.97   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Cranberry     38.13   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Strawberry    42.32   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(2.0922) family taken to be 1)
## 
##     Null deviance: 90823  on 1674  degrees of freedom
## Residual deviance:  1892  on 1653  degrees of freedom
## AIC: 14361
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  2.0922 
##           Std. Err.:  0.0780 
## 
##  2 x log-likelihood:  -14316.5790
## G0: no difference among different fruit media
## G7: fitness increase for strawberry populations on strawberry, but not on cherry or cranberry fruit media
## G7: no fitness change for cherry or cranberry populations

## G29: fitness increase for cherry populations on cherry medium, but not on strawberry or cranberry medium
## G29: fitness increase for strawberry populations on strawberry medium, but no change on cranberry medium and fitness decrease on cherry medium
## G29: fitness increase for cranberry populations on cranberry and cherry media, and fitness decrease on strawberry medium
library(lme4)
m2 <- lme4::glmer.nb(Nb_adults ~ -1 + Generation_Fruit_s_Treatment + (1|Line), data = data, verbose=TRUE)
## th := est_theta(glmer(..)) = 2.201538 --> dev.= -2*logLik(.) = 14290.06 
##  1: th=    1.084318957, dev=14594.96208819, beta[1]=    3.21950449
##  2: th=    4.469872861, dev=14690.23715275, beta[1]=    3.21930319
##  3: th=   0.4518359525, dev=15686.75531922, beta[1]=    3.21972111
##  4: th=    2.076154386, dev=14291.25743655, beta[1]=    3.22959061
##  5: th=    2.097889997, dev=14290.67362916, beta[1]=    3.21837130
##  6: th=    2.182950839, dev=14289.93574613, beta[1]=    3.21843046
##  7: th=    2.870326763, dev=14346.85616883, beta[1]=    3.21836162
##  8: th=    2.166741409, dev=14289.90664279, beta[1]=    3.21857142
##  9: th=    2.168737290, dev=14289.90604266, beta[1]=    3.21841254
## 10: th=    2.168773461, dev=14289.90604248, beta[1]=    3.21841248
## 11: th=    2.168809633, dev=14289.90604274, beta[1]=    3.21841325
## 12: th=    2.168773461, dev=14289.90604248, beta[1]=    3.21840813
summary(m2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(2.1688)  ( log )
## Formula: Nb_adults ~ -1 + Generation_Fruit_s_Treatment + (1 | Line)
##    Data: data
## 
##      AIC      BIC   logLik deviance df.resid 
##  14335.9  14460.6  -7145.0  14289.9     1651 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.4393 -0.6310 -0.0589  0.5331  8.2849 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Line   (Intercept) 0.02049  0.1431  
## Number of obs: 1674, groups:  Line, 26
## 
## Fixed effects:
##                                                      Estimate Std. Error
## Generation_Fruit_s_Treatment0_GF_Cherry               3.21841    0.15968
## Generation_Fruit_s_Treatment0_GF_Cranberry            3.26379    0.15962
## Generation_Fruit_s_Treatment0_GF_Strawberry           3.35440    0.15952
## Generation_Fruit_s_Treatment29_Cherry_Cherry          3.47413    0.11094
## Generation_Fruit_s_Treatment29_Cherry_Cranberry       2.93116    0.11206
## Generation_Fruit_s_Treatment29_Cherry_Strawberry      3.38072    0.11106
## Generation_Fruit_s_Treatment29_Cranberry_Cherry       3.46660    0.08637
## Generation_Fruit_s_Treatment29_Cranberry_Cranberry    3.44527    0.08626
## Generation_Fruit_s_Treatment29_Cranberry_Strawberry   2.97878    0.08685
## Generation_Fruit_s_Treatment29_Strawberry_Cherry      3.23660    0.11148
## Generation_Fruit_s_Treatment29_Strawberry_Cranberry   3.38206    0.11123
## Generation_Fruit_s_Treatment29_Strawberry_Strawberry  3.72384    0.11068
## Generation_Fruit_s_Treatment7_Cherry_Cherry           3.19445    0.14794
## Generation_Fruit_s_Treatment7_Cherry_Cranberry        3.09115    0.15478
## Generation_Fruit_s_Treatment7_Cherry_Strawberry       3.50488    0.15520
## Generation_Fruit_s_Treatment7_Cranberry_Cherry        3.20505    0.13989
## Generation_Fruit_s_Treatment7_Cranberry_Cranberry     3.44650    0.12843
## Generation_Fruit_s_Treatment7_Cranberry_Strawberry    3.60078    0.14213
## Generation_Fruit_s_Treatment7_Strawberry_Cherry       3.67840    0.11704
## Generation_Fruit_s_Treatment7_Strawberry_Cranberry    3.53198    0.12019
## Generation_Fruit_s_Treatment7_Strawberry_Strawberry   3.68609    0.11504
##                                                      z value Pr(>|z|)    
## Generation_Fruit_s_Treatment0_GF_Cherry                20.16   <2e-16 ***
## Generation_Fruit_s_Treatment0_GF_Cranberry             20.45   <2e-16 ***
## Generation_Fruit_s_Treatment0_GF_Strawberry            21.03   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Cherry           31.32   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Cranberry        26.16   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Strawberry       30.44   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Cherry        40.14   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Cranberry     39.94   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Strawberry    34.30   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Cherry       29.03   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Cranberry    30.41   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Strawberry   33.64   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Cherry            21.59   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Cranberry         19.97   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Strawberry        22.58   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Cherry         22.91   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Cranberry      26.84   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Strawberry     25.34   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Cherry        31.43   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Cranberry     29.39   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Strawberry    32.04   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
MuMIn::model.sel(m0, m1, m2)
## Model selection table 
##    (Int) Frt_s Gnr Frt_s:Gnr Gnr_Frt_s_Trt                        family
## m2                                       + Negative Binomial(2.1688,log)
## m1                                       + Negative Binomial(2.0922,log)
## m0 3.281     +   +         +               Negative Binomial(1.9682,log)
##       class init.theta link random df    logLik    AICc delta weight
## m2 glmerMod                      L 23 -7144.953 14336.6  0.00      1
## m1   negbin       2.09  log        22 -7158.289 14361.2 24.62      0
## m0   negbin       1.97  log         8 -7206.144 14428.4 91.80      0
## Models ranked by AICc(x) 
## Random terms: 
## L = '1 | Line'
## Get overdispersion parameter
getME(m2, "glmer.nb.theta")
## [1] 2.168773
## Check residuals for the different experimental populations
plot(m2, resid(.) ~ as.numeric(Line))

## Check random effect
# sjPlot::plot_model(m2, type="re", dot.size=1.5, 
#                    group.terms = c(1, rep(2, nlevels(data_G7$Line)), 
#                                    rep(3, nlevels(data_G29$Line))))

mySumm <- function(.) {
    c(beta=c(fixef(.)[1:3], fixef(.)[13:21] - rep(fixef(.)[1:3], 3), fixef(.)[4:12] - rep(fixef(.)[1:3], 3)), sigma=sqrt(unlist(VarCorr(.))))
}

boo01 <- lme4::bootMer(m2, mySumm, nsim = 500, re.form = NA)

head(data.frame(boo01))
##   beta.Generation_Fruit_s_Treatment0_GF_Cherry
## 1                                     3.389688
## 2                                     3.376950
## 3                                     3.388896
## 4                                     3.286045
## 5                                     3.196028
## 6                                     3.072877
##   beta.Generation_Fruit_s_Treatment0_GF_Cranberry
## 1                                        3.393543
## 2                                        3.395640
## 3                                        3.371223
## 4                                        3.390066
## 5                                        3.181559
## 6                                        3.095099
##   beta.Generation_Fruit_s_Treatment0_GF_Strawberry
## 1                                         3.476437
## 2                                         3.531176
## 3                                         3.526393
## 4                                         3.472006
## 5                                         3.303237
## 6                                         3.181598
##   beta.Generation_Fruit_s_Treatment7_Cherry_Cherry
## 1                                      -0.15131529
## 2                                      -0.29875885
## 3                                      -0.27911892
## 4                                      -0.04208316
## 5                                       0.04068846
## 6                                       0.11389407
##   beta.Generation_Fruit_s_Treatment7_Cherry_Cranberry
## 1                                          -0.2421151
## 2                                          -0.2980911
## 3                                          -0.5195561
## 4                                           0.1152746
## 5                                          -0.2326500
## 6                                           0.1356246
##   beta.Generation_Fruit_s_Treatment7_Cherry_Strawberry
## 1                                         -0.007563546
## 2                                         -0.078876450
## 3                                         -0.051932924
## 4                                          0.207733832
## 5                                          0.192053488
## 6                                          0.283420285
##   beta.Generation_Fruit_s_Treatment7_Cranberry_Cherry
## 1                                        -0.270361150
## 2                                        -0.074064317
## 3                                        -0.250297033
## 4                                         0.039107464
## 5                                        -0.002330064
## 6                                         0.117256084
##   beta.Generation_Fruit_s_Treatment7_Cranberry_Cranberry
## 1                                             0.03029992
## 2                                             0.16309213
## 3                                            -0.17846913
## 4                                             0.20206953
## 5                                             0.08567410
## 6                                             0.40179789
##   beta.Generation_Fruit_s_Treatment7_Cranberry_Strawberry
## 1                                             -0.06624386
## 2                                              0.14028317
## 3                                              0.02187438
## 4                                              0.12342269
## 5                                              0.26190644
## 6                                              0.32417088
##   beta.Generation_Fruit_s_Treatment7_Strawberry_Cherry
## 1                                            0.3576928
## 2                                            0.2921236
## 3                                            0.4709825
## 4                                            0.2733298
## 5                                            0.5589026
## 6                                            0.8325958
##   beta.Generation_Fruit_s_Treatment7_Strawberry_Cranberry
## 1                                             0.006228872
## 2                                            -0.079477457
## 3                                             0.212355194
## 4                                            -0.019436387
## 5                                             0.436972618
## 6                                             0.609439874
##   beta.Generation_Fruit_s_Treatment7_Strawberry_Strawberry
## 1                                                0.1209654
## 2                                                0.1473214
## 3                                                0.3505781
## 4                                               -0.1061980
## 5                                                0.5711472
## 6                                                0.5206647
##   beta.Generation_Fruit_s_Treatment29_Cherry_Cherry
## 1                                        0.18354186
## 2                                        0.12744675
## 3                                       -0.09596111
## 4                                        0.17003334
## 5                                        0.22634791
## 6                                        0.55017236
##   beta.Generation_Fruit_s_Treatment29_Cherry_Cranberry
## 1                                          -0.48152101
## 2                                          -0.46714477
## 3                                          -0.56908366
## 4                                          -0.32994792
## 5                                          -0.22693093
## 6                                          -0.00993869
##   beta.Generation_Fruit_s_Treatment29_Cherry_Strawberry
## 1                                           0.006403419
## 2                                          -0.060610653
## 3                                          -0.177856250
## 4                                           0.059111269
## 5                                           0.110419297
## 6                                           0.351542187
##   beta.Generation_Fruit_s_Treatment29_Cranberry_Cherry
## 1                                         0.0004601943
## 2                                         0.1156391772
## 3                                         0.1054431315
## 4                                         0.2370840409
## 5                                         0.2347706641
## 6                                         0.4330759518
##   beta.Generation_Fruit_s_Treatment29_Cranberry_Cranberry
## 1                                              0.03565671
## 2                                             -0.03133405
## 3                                              0.13905999
## 4                                              0.01422908
## 5                                              0.30556058
## 6                                              0.25281926
##   beta.Generation_Fruit_s_Treatment29_Cranberry_Strawberry
## 1                                               -0.5114095
## 2                                               -0.6552197
## 3                                               -0.4789206
## 4                                               -0.5481690
## 5                                               -0.3782189
## 6                                               -0.0847164
##   beta.Generation_Fruit_s_Treatment29_Strawberry_Cherry
## 1                                           -0.14699944
## 2                                           -0.24249834
## 3                                           -0.16481348
## 4                                           -0.17347761
## 5                                            0.19715214
## 6                                            0.04479471
##   beta.Generation_Fruit_s_Treatment29_Strawberry_Cranberry
## 1                                              -0.06805847
## 2                                              -0.08591012
## 3                                               0.09135879
## 4                                              -0.11043459
## 5                                               0.22683881
## 6                                               0.19803686
##   beta.Generation_Fruit_s_Treatment29_Strawberry_Strawberry sigma.Line
## 1                                                0.34945710 0.09837693
## 2                                                0.08106770 0.08077395
## 3                                                0.09462827 0.14427560
## 4                                                0.28616943 0.10234151
## 5                                                0.53310257 0.14807569
## 6                                                0.29639931 0.14933413
## Extract all CIs
bCI_tab(boo01)
##                                                              Estimate
## beta.Generation_Fruit_s_Treatment0_GF_Cherry               3.21840813
## beta.Generation_Fruit_s_Treatment0_GF_Cranberry            3.26378671
## beta.Generation_Fruit_s_Treatment0_GF_Strawberry           3.35439907
## beta.Generation_Fruit_s_Treatment7_Cherry_Cherry          -0.02396108
## beta.Generation_Fruit_s_Treatment7_Cherry_Cranberry       -0.17263265
## beta.Generation_Fruit_s_Treatment7_Cherry_Strawberry       0.15048578
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cherry       -0.01335955
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cranberry     0.18271763
## beta.Generation_Fruit_s_Treatment7_Cranberry_Strawberry    0.24637782
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cherry       0.45999087
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cranberry    0.26818924
## beta.Generation_Fruit_s_Treatment7_Strawberry_Strawberry   0.33168853
## beta.Generation_Fruit_s_Treatment29_Cherry_Cherry          0.25572536
## beta.Generation_Fruit_s_Treatment29_Cherry_Cranberry      -0.33262760
## beta.Generation_Fruit_s_Treatment29_Cherry_Strawberry      0.02632313
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cherry       0.24819431
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cranberry    0.18147832
## beta.Generation_Fruit_s_Treatment29_Cranberry_Strawberry  -0.37561420
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cherry      0.01818883
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cranberry   0.11827014
## beta.Generation_Fruit_s_Treatment29_Strawberry_Strawberry  0.36944053
## sigma.Line                                                 0.14312861
##                                                                 X2.5..
## beta.Generation_Fruit_s_Treatment0_GF_Cherry               2.942776469
## beta.Generation_Fruit_s_Treatment0_GF_Cranberry            2.996934894
## beta.Generation_Fruit_s_Treatment0_GF_Strawberry           3.096732793
## beta.Generation_Fruit_s_Treatment7_Cherry_Cherry          -0.460836284
## beta.Generation_Fruit_s_Treatment7_Cherry_Cranberry       -0.603091163
## beta.Generation_Fruit_s_Treatment7_Cherry_Strawberry      -0.299635582
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cherry       -0.445294794
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cranberry    -0.209674359
## beta.Generation_Fruit_s_Treatment7_Cranberry_Strawberry   -0.158793709
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cherry       0.068696585
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cranberry   -0.142499675
## beta.Generation_Fruit_s_Treatment7_Strawberry_Strawberry  -0.055984513
## beta.Generation_Fruit_s_Treatment29_Cherry_Cherry         -0.158702712
## beta.Generation_Fruit_s_Treatment29_Cherry_Cranberry      -0.700439850
## beta.Generation_Fruit_s_Treatment29_Cherry_Strawberry     -0.333814762
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cherry      -0.089445887
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cranberry   -0.139025997
## beta.Generation_Fruit_s_Treatment29_Cranberry_Strawberry  -0.701155686
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cherry     -0.360277110
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cranberry  -0.270641171
## beta.Generation_Fruit_s_Treatment29_Strawberry_Strawberry  0.002465931
## sigma.Line                                                 0.010502500
##                                                               X97.5..
## beta.Generation_Fruit_s_Treatment0_GF_Cherry               3.49903888
## beta.Generation_Fruit_s_Treatment0_GF_Cranberry            3.52494224
## beta.Generation_Fruit_s_Treatment0_GF_Strawberry           3.61072457
## beta.Generation_Fruit_s_Treatment7_Cherry_Cherry           0.39927346
## beta.Generation_Fruit_s_Treatment7_Cherry_Cranberry        0.27071914
## beta.Generation_Fruit_s_Treatment7_Cherry_Strawberry       0.55176419
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cherry        0.38081092
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cranberry     0.55986335
## beta.Generation_Fruit_s_Treatment7_Cranberry_Strawberry    0.63189757
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cherry       0.85822235
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cranberry    0.65218304
## beta.Generation_Fruit_s_Treatment7_Strawberry_Strawberry   0.69838567
## beta.Generation_Fruit_s_Treatment29_Cherry_Cherry          0.60878103
## beta.Generation_Fruit_s_Treatment29_Cherry_Cranberry       0.00613693
## beta.Generation_Fruit_s_Treatment29_Cherry_Strawberry      0.38074466
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cherry       0.58915695
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cranberry    0.51159660
## beta.Generation_Fruit_s_Treatment29_Cranberry_Strawberry  -0.05311594
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cherry      0.40559334
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cranberry   0.51378585
## beta.Generation_Fruit_s_Treatment29_Strawberry_Strawberry  0.71092561
## sigma.Line                                                 0.16899310
## G0: no difference among different fruit media (beta.TreatmentrelCherry / beta.TreatmentrelCranberry include zero)
## G7: no fitness change for strawberry, cherry or cranberry populations
## G29: fitness increase for cherry populations on cherry medium, but not on strawberry or cranberry medium

## G29: fitness increase for cranberry populations on cranberry and cherry media, and fitness decrease on strawberry medium
## G29: strawberry population with no change on strawberry or cranberry medium and fitness decrease on cherry medium

# BACKUP
# mfitness_fruits <- lme4::glmer.nb(Nb_adults ~ -1 + Treatment + Fruit_s:Treatment:Generation 
#                                   +  (1|Line), data=data)
# summary(mfitness_fruits)
# CIfitness <- confint(mfitness_fruits)

3.5 Analysis fecundity

3.5.1 Compare fit of Poisson lognormal and negative binomial distributions

## Poisson lognormal model
mpoislognormal <- lme4::glmer(Nb_eggs ~ Generation*Fruit_s*Treatment + (1|Obs), family="poisson", data=data)
summary(mpoislognormal)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: poisson  ( log )
## Formula: Nb_eggs ~ Generation * Fruit_s * Treatment + (1 | Obs)
##    Data: data
## 
##      AIC      BIC   logLik deviance df.resid 
##  17768.2  17887.5  -8862.1  17724.2     1652 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.3733 -0.1321  0.0278  0.1410  0.4241 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Obs    (Intercept) 0.1395   0.3735  
## Number of obs: 1674, groups:  Obs, 1674
## 
## Fixed effects:
##                                                     Estimate Std. Error z value
## (Intercept)                                         4.952094   0.038337 129.172
## Generation29                                       -0.406577   0.056129  -7.244
## Generation7                                         0.249046   0.077600   3.209
## Fruit_sCranberry                                   -0.021392   0.093958  -0.228
## Fruit_sStrawberry                                  -0.051014   0.083309  -0.612
## TreatmentCranberry                                 -0.209939   0.054382  -3.860
## TreatmentStrawberry                                -0.345191   0.054507  -6.333
## Generation29:Fruit_sCranberry                       0.121143   0.107268   1.129
## Generation29:Fruit_sStrawberry                     -0.015555   0.101500  -0.153
## Generation29:TreatmentCranberry                     0.447408   0.079279   5.643
## Generation7:TreatmentCranberry                      0.029638   0.113040   0.262
## Generation29:TreatmentStrawberry                    0.635715   0.079331   8.013
## Generation7:TreatmentStrawberry                     0.236696   0.113721   2.081
## Fruit_sCranberry:TreatmentCranberry                 0.116394   0.131984   0.882
## Fruit_sStrawberry:TreatmentCranberry                0.230489   0.121315   1.900
## Fruit_sCranberry:TreatmentStrawberry               -0.196064   0.135897  -1.443
## Fruit_sStrawberry:TreatmentStrawberry               0.037818   0.120836   0.313
## Generation29:Fruit_sCranberry:TreatmentCranberry   -0.243557   0.150783  -1.615
## Generation29:Fruit_sStrawberry:TreatmentCranberry  -0.322735   0.146260  -2.207
## Generation29:Fruit_sCranberry:TreatmentStrawberry   0.034852   0.154195   0.226
## Generation29:Fruit_sStrawberry:TreatmentStrawberry  0.005531   0.145764   0.038
##                                                    Pr(>|z|)    
## (Intercept)                                         < 2e-16 ***
## Generation29                                       4.37e-13 ***
## Generation7                                        0.001330 ** 
## Fruit_sCranberry                                   0.819895    
## Fruit_sStrawberry                                  0.540308    
## TreatmentCranberry                                 0.000113 ***
## TreatmentStrawberry                                2.40e-10 ***
## Generation29:Fruit_sCranberry                      0.258752    
## Generation29:Fruit_sStrawberry                     0.878199    
## Generation29:TreatmentCranberry                    1.67e-08 ***
## Generation7:TreatmentCranberry                     0.793178    
## Generation29:TreatmentStrawberry                   1.12e-15 ***
## Generation7:TreatmentStrawberry                    0.037401 *  
## Fruit_sCranberry:TreatmentCranberry                0.377842    
## Fruit_sStrawberry:TreatmentCranberry               0.057444 .  
## Fruit_sCranberry:TreatmentStrawberry               0.149094    
## Fruit_sStrawberry:TreatmentStrawberry              0.754302    
## Generation29:Fruit_sCranberry:TreatmentCranberry   0.106250    
## Generation29:Fruit_sStrawberry:TreatmentCranberry  0.027344 *  
## Generation29:Fruit_sCranberry:TreatmentStrawberry  0.821183    
## Generation29:Fruit_sStrawberry:TreatmentStrawberry 0.969729    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## fit warnings:
## fixed-effect model matrix is rank deficient so dropping 15 columns / coefficients
## convergence code: 0
## Model failed to converge with max|grad| = 0.00706297 (tol = 0.002, component 1)
## Check convergence
derivs1 <- mpoislognormal@optinfo$derivs
sc_grad1 <- with(derivs1,solve(Hessian,gradient))
max(abs(sc_grad1))
## [1] 0.001258741
## Negative binomial model
mnegbin <- MASS::glm.nb(Nb_eggs ~ Generation*Fruit_s*Treatment, data=data)

## Compare AIC
AIC(mnegbin, mpoislognormal)
##                df      AIC
## mnegbin        22 17605.09
## mpoislognormal 22 17768.21
## Simulate data with Poisson lognormal distribution
x.teo.poislognormal <- unlist(simulate(mpoislognormal))
x.teo.negbin <- unlist(simulate(mnegbin))


## QQplot to compared Negative binomial and Poisson log normal distributions
qqplot(data$Nb_eggs, x.teo.negbin, main="QQ-plot", xlab="Observed data", ylab="Simulated data", las=1, xlim = c(0, 100), ylim = c(0, 100)) ## QQ-plot
points(sort(data$Nb_eggs), sort(x.teo.poislognormal), pch=1, col='red')
abline(0,1)
## Add legend
legend(0, 70, legend=c("Neg. Bin.", "Pois. Lognormal"), col=c("black", "red"), pch=1, bty="n")

## Negative binomial distribution provides a better fit to the data

3.5.2 Compare changes in fecundity

m0 <- MASS::glm.nb(Nb_eggs ~ Generation*Fruit_s, data=data)
summary(m0)
## 
## Call:
## MASS::glm.nb(formula = Nb_eggs ~ Generation * Fruit_s, data = data, 
##     init.theta = 7.377024136, link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -5.7045  -0.7038  -0.0426   0.5555   3.0911  
## 
## Coefficients: (5 not defined because of singularities)
##                                 Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                     4.842033   0.021867 221.433  < 2e-16 ***
## Generation29                   -0.055158   0.031798  -1.735   0.0828 .  
## Generation7                     0.364938   0.045817   7.965 1.65e-15 ***
## Fruit_sCranberry               -0.060454   0.053593  -1.128   0.2593    
## Fruit_sGF                             NA         NA      NA       NA    
## Fruit_sStrawberry               0.005185   0.048861   0.106   0.9155    
## Generation29:Fruit_sCranberry   0.056394   0.061033   0.924   0.3555    
## Generation7:Fruit_sCranberry          NA         NA      NA       NA    
## Generation29:Fruit_sGF                NA         NA      NA       NA    
## Generation7:Fruit_sGF                 NA         NA      NA       NA    
## Generation29:Fruit_sStrawberry -0.095729   0.058790  -1.628   0.1035    
## Generation7:Fruit_sStrawberry         NA         NA      NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(7.377) family taken to be 1)
## 
##     Null deviance: 2129.0  on 1673  degrees of freedom
## Residual deviance: 1728.7  on 1667  degrees of freedom
## AIC: 17690
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  7.377 
##           Std. Err.:  0.268 
## 
##  2 x log-likelihood:  -17673.630
m1 <- MASS::glm.nb(Nb_eggs ~ -1 + Generation_Fruit_s_Treatment, data=data)
summary(m1)
## 
## Call:
## MASS::glm.nb(formula = Nb_eggs ~ -1 + Generation_Fruit_s_Treatment, 
##     data = data, init.theta = 7.913400412, link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -5.6762  -0.6836  -0.0461   0.5447   3.0625  
## 
## Coefficients:
##                                                      Estimate Std. Error
## Generation_Fruit_s_Treatment0_GF_Cherry               5.00723    0.03648
## Generation_Fruit_s_Treatment0_GF_Cranberry            4.81096    0.03668
## Generation_Fruit_s_Treatment0_GF_Strawberry           4.68065    0.03683
## Generation_Fruit_s_Treatment29_Cherry_Cherry          4.64214    0.03887
## Generation_Fruit_s_Treatment29_Cherry_Cranberry       4.82671    0.03864
## Generation_Fruit_s_Treatment29_Cherry_Strawberry      4.87681    0.03858
## Generation_Fruit_s_Treatment29_Cranberry_Cherry       4.70272    0.03005
## Generation_Fruit_s_Treatment29_Cranberry_Cranberry    4.81786    0.02994
## Generation_Fruit_s_Treatment29_Cranberry_Strawberry   4.82329    0.02993
## Generation_Fruit_s_Treatment29_Strawberry_Cherry      4.53391    0.03903
## Generation_Fruit_s_Treatment29_Strawberry_Cranberry   4.68542    0.03882
## Generation_Fruit_s_Treatment29_Strawberry_Strawberry  4.84541    0.03862
## Generation_Fruit_s_Treatment7_Cherry_Cherry           5.27604    0.06410
## Generation_Fruit_s_Treatment7_Cherry_Cranberry        5.16458    0.06868
## Generation_Fruit_s_Treatment7_Cherry_Strawberry       5.16436    0.06994
## Generation_Fruit_s_Treatment7_Cranberry_Cherry        5.22401    0.06225
## Generation_Fruit_s_Treatment7_Cranberry_Cranberry     5.19938    0.05475
## Generation_Fruit_s_Treatment7_Cranberry_Strawberry    4.98810    0.06169
## Generation_Fruit_s_Treatment7_Strawberry_Cherry       5.20931    0.04649
## Generation_Fruit_s_Treatment7_Strawberry_Cranberry    5.26848    0.04762
## Generation_Fruit_s_Treatment7_Strawberry_Strawberry   5.16188    0.04508
##                                                      z value Pr(>|z|)    
## Generation_Fruit_s_Treatment0_GF_Cherry               137.27   <2e-16 ***
## Generation_Fruit_s_Treatment0_GF_Cranberry            131.18   <2e-16 ***
## Generation_Fruit_s_Treatment0_GF_Strawberry           127.09   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Cherry          119.42   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Cranberry       124.91   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Strawberry      126.39   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Cherry       156.50   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Cranberry    160.92   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Strawberry   161.13   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Cherry      116.16   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Cranberry   120.71   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Strawberry  125.47   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Cherry            82.31   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Cranberry         75.19   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Strawberry        73.84   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Cherry         83.92   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Cranberry      94.97   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Strawberry     80.86   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Cherry       112.06   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Cranberry    110.64   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Strawberry   114.50   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(7.9134) family taken to be 1)
## 
##     Null deviance: 878081.8  on 1674  degrees of freedom
## Residual deviance:   1728.5  on 1653  degrees of freedom
## AIC: 17605
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  7.913 
##           Std. Err.:  0.289 
## 
##  2 x log-likelihood:  -17561.088
m2 <- lme4::glmer.nb(Nb_eggs ~ -1 + Generation_Fruit_s_Treatment + (1|Line), data = data, verbose=TRUE)
## th := est_theta(glmer(..)) = 8.025924 --> dev.= -2*logLik(.) = 17561.24 
##  1: th=    3.952992494, dev=17870.35338397, beta[1]=    5.00722950
##  2: th=    16.29536564, dev=18018.18209852, beta[1]=    5.00722217
##  3: th=    1.647212859, dev=18883.11041099, beta[1]=    5.00708268
##  4: th=    7.299870710, dev=17565.88051763, beta[1]=    5.00724752
##  5: th=    7.485156715, dev=17563.37873585, beta[1]=    5.00723026
##  6: th=    8.010336468, dev=17561.19953493, beta[1]=    5.00722623
##  7: th=    7.915948240, dev=17561.08823172, beta[1]=    5.00722694
##  8: th=    7.912782631, dev=17561.08815853, beta[1]=    5.00722782
##  9: th=    7.913401651, dev=17561.08815300, beta[1]=    5.00723133
## 10: th=    7.913533786, dev=17561.08815316, beta[1]=    5.00723041
## 11: th=    7.913269518, dev=17561.08815315, beta[1]=    5.00723012
## 12: th=    7.913401651, dev=17561.08815293, beta[1]=    5.00723035
summary(m2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(7.9134)  ( log )
## Formula: Nb_eggs ~ -1 + Generation_Fruit_s_Treatment + (1 | Line)
##    Data: data
## 
##      AIC      BIC   logLik deviance df.resid 
##  17607.1  17731.8  -8780.5  17561.1     1651 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -2.6593 -0.6294 -0.0458  0.5804  4.2617 
## 
## Random effects:
##  Groups Name        Variance  Std.Dev. 
##  Line   (Intercept) 2.343e-12 1.531e-06
## Number of obs: 1674, groups:  Line, 26
## 
## Fixed effects:
##                                                      Estimate Std. Error
## Generation_Fruit_s_Treatment0_GF_Cherry               5.00723    0.03648
## Generation_Fruit_s_Treatment0_GF_Cranberry            4.81097    0.03668
## Generation_Fruit_s_Treatment0_GF_Strawberry           4.68065    0.03683
## Generation_Fruit_s_Treatment29_Cherry_Cherry          4.64214    0.03887
## Generation_Fruit_s_Treatment29_Cherry_Cranberry       4.82671    0.03864
## Generation_Fruit_s_Treatment29_Cherry_Strawberry      4.87681    0.03858
## Generation_Fruit_s_Treatment29_Cranberry_Cherry       4.70272    0.03005
## Generation_Fruit_s_Treatment29_Cranberry_Cranberry    4.81786    0.02994
## Generation_Fruit_s_Treatment29_Cranberry_Strawberry   4.82329    0.02993
## Generation_Fruit_s_Treatment29_Strawberry_Cherry      4.53391    0.03903
## Generation_Fruit_s_Treatment29_Strawberry_Cranberry   4.68542    0.03882
## Generation_Fruit_s_Treatment29_Strawberry_Strawberry  4.84541    0.03862
## Generation_Fruit_s_Treatment7_Cherry_Cherry           5.27604    0.06410
## Generation_Fruit_s_Treatment7_Cherry_Cranberry        5.16458    0.06868
## Generation_Fruit_s_Treatment7_Cherry_Strawberry       5.16436    0.06994
## Generation_Fruit_s_Treatment7_Cranberry_Cherry        5.22401    0.06225
## Generation_Fruit_s_Treatment7_Cranberry_Cranberry     5.19938    0.05475
## Generation_Fruit_s_Treatment7_Cranberry_Strawberry    4.98810    0.06169
## Generation_Fruit_s_Treatment7_Strawberry_Cherry       5.20930    0.04649
## Generation_Fruit_s_Treatment7_Strawberry_Cranberry    5.26848    0.04762
## Generation_Fruit_s_Treatment7_Strawberry_Strawberry   5.16188    0.04508
##                                                      z value Pr(>|z|)    
## Generation_Fruit_s_Treatment0_GF_Cherry               137.27   <2e-16 ***
## Generation_Fruit_s_Treatment0_GF_Cranberry            131.18   <2e-16 ***
## Generation_Fruit_s_Treatment0_GF_Strawberry           127.09   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Cherry          119.42   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Cranberry       124.91   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Strawberry      126.39   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Cherry       156.50   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Cranberry    160.92   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Strawberry   161.13   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Cherry      116.16   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Cranberry   120.71   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Strawberry  125.47   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Cherry            82.31   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Cranberry         75.19   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Strawberry        73.84   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Cherry         83.92   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Cranberry      94.97   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Strawberry     80.86   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Cherry       112.06   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Cranberry    110.64   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Strawberry   114.50   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## convergence code: 0
## boundary (singular) fit: see ?isSingular
MuMIn::model.sel(m0, m1, m2)
## Model selection table 
##    (Int) Frt_s Gnr Frt_s:Gnr Gnr_Frt_s_Trt                        family
## m1                                       + Negative Binomial(7.9134,log)
## m2                                       + Negative Binomial(7.9134,log)
## m0 4.842     +   +         +                Negative Binomial(7.377,log)
##       class init.theta link random df    logLik    AICc delta weight
## m1   negbin       7.91  log        22 -8780.544 17605.7  0.00  0.737
## m2 glmerMod                      L 23 -8780.544 17607.8  2.06  0.263
## m0   negbin       7.38  log         8 -8836.815 17689.7 84.02  0.000
## Models ranked by AICc(x) 
## Random terms: 
## L = '1 | Line'
m3 <- lme4::glmer.nb(Nb_eggs ~ Generation*Fruit_s*Treatmentrel + (1|Line), data = data, verbose=TRUE)
## th := est_theta(glmer(..)) = 8.025924 --> dev.= -2*logLik(.) = 17561.24 
##  1: th=    3.952992508, dev=17870.35338114, beta[1]=    4.68064865
##  2: th=    16.29536569, dev=18018.18821701, beta[1]=    4.68039503
##  3: th=    1.647212864, dev=18883.11885152, beta[1]=    4.68114587
##  4: th=    7.299849738, dev=17565.88420808, beta[1]=    4.68135311
##  5: th=    7.485133796, dev=17563.37978383, beta[1]=    4.68079517
##  6: th=    8.011172818, dev=17561.20174286, beta[1]=    4.68066248
##  7: th=    7.915734061, dev=17561.08843739, beta[1]=    4.68063970
##  8: th=    7.912764007, dev=17561.08833842, beta[1]=    4.68061126
##  9: th=    7.912896131, dev=17561.08831283, beta[1]=    4.68065174
## 10: th=    7.914040567, dev=17561.08827176, beta[1]=    4.68052128
## 11: th=    7.914687382, dev=17561.08826426, beta[1]=    4.68063195
## 12: th=    7.914819538, dev=17561.08824761, beta[1]=    4.68064901
## 13: th=    7.915168842, dev=17561.08825232, beta[1]=    4.68060105
## 14: th=    7.914951697, dev=17561.08824134, beta[1]=    4.68060926
## 15: th=    7.914951697, dev=17561.08823881, beta[1]=    4.68062832
summary(m3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: Negative Binomial(7.915)  ( log )
## Formula: Nb_eggs ~ Generation * Fruit_s * Treatmentrel + (1 | Line)
##    Data: data
## 
##      AIC      BIC   logLik deviance df.resid 
##  17607.1  17731.8  -8780.5  17561.1     1651 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -2.6596 -0.6295 -0.0459  0.5804  4.2620 
## 
## Random effects:
##  Groups Name        Variance  Std.Dev.
##  Line   (Intercept) 1.123e-08 0.000106
## Number of obs: 1674, groups:  Line, 26
## 
## Fixed effects:
##                                                       Estimate Std. Error
## (Intercept)                                           4.680628   0.036826
## Generation29                                          0.196194   0.053336
## Generation7                                           0.483777   0.079041
## Fruit_sCranberry                                     -0.176303   0.093253
## Fruit_sStrawberry                                    -0.002522   0.083207
## TreatmentrelCherry                                    0.326621   0.051832
## TreatmentrelCranberry                                 0.130346   0.051971
## Generation29:Fruit_sCranberry                         0.122761   0.105265
## Generation29:Fruit_sStrawberry                       -0.028881   0.099516
## Generation29:TreatmentrelCherry                      -0.561288   0.075406
## Generation7:TreatmentrelCherry                       -0.215013   0.108101
## Generation29:TreatmentrelCranberry                   -0.180438   0.075382
## Generation7:TreatmentrelCranberry                    -0.130205   0.110944
## Fruit_sCranberry:TreatmentrelCherry                   0.124316   0.129147
## Fruit_sStrawberry:TreatmentrelCherry                 -0.064182   0.114858
## Fruit_sCranberry:TreatmentrelCranberry                0.211146   0.128098
## Fruit_sStrawberry:TreatmentrelCranberry               0.106461   0.117928
## Generation29:Fruit_sCranberry:TreatmentrelCherry     -0.010215   0.146552
## Generation29:Fruit_sStrawberry:TreatmentrelCherry    -0.012671   0.138588
## Generation29:Fruit_sCranberry:TreatmentrelCranberry  -0.166474   0.145544
## Generation29:Fruit_sStrawberry:TreatmentrelCranberry -0.216371   0.141019
##                                                      z value Pr(>|z|)    
## (Intercept)                                          127.101  < 2e-16 ***
## Generation29                                           3.678 0.000235 ***
## Generation7                                            6.121 9.32e-10 ***
## Fruit_sCranberry                                      -1.891 0.058680 .  
## Fruit_sStrawberry                                     -0.030 0.975825    
## TreatmentrelCherry                                     6.302 2.95e-10 ***
## TreatmentrelCranberry                                  2.508 0.012140 *  
## Generation29:Fruit_sCranberry                          1.166 0.243531    
## Generation29:Fruit_sStrawberry                        -0.290 0.771648    
## Generation29:TreatmentrelCherry                       -7.444 9.80e-14 ***
## Generation7:TreatmentrelCherry                        -1.989 0.046702 *  
## Generation29:TreatmentrelCranberry                    -2.394 0.016681 *  
## Generation7:TreatmentrelCranberry                     -1.174 0.240548    
## Fruit_sCranberry:TreatmentrelCherry                    0.963 0.335753    
## Fruit_sStrawberry:TreatmentrelCherry                  -0.559 0.576301    
## Fruit_sCranberry:TreatmentrelCranberry                 1.648 0.099289 .  
## Fruit_sStrawberry:TreatmentrelCranberry                0.903 0.366652    
## Generation29:Fruit_sCranberry:TreatmentrelCherry      -0.070 0.944429    
## Generation29:Fruit_sStrawberry:TreatmentrelCherry     -0.091 0.927153    
## Generation29:Fruit_sCranberry:TreatmentrelCranberry   -1.144 0.252704    
## Generation29:Fruit_sStrawberry:TreatmentrelCranberry  -1.534 0.124947    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## fit warnings:
## fixed-effect model matrix is rank deficient so dropping 15 columns / coefficients
## convergence code: 0
## Model failed to converge with max|grad| = 0.010463 (tol = 0.002, component 1)
## Get overdispersion parameter
getME(m2, "glmer.nb.theta")
## [1] 7.913402
## Check residuals for the different experimental populations
plot(m2, resid(.) ~ as.numeric(Line))

boo01 <- lme4::bootMer(m2, mySumm, nsim = 50, re.form = NA)

head(data.frame(boo01))
##   beta.Generation_Fruit_s_Treatment0_GF_Cherry
## 1                                     5.106916
## 2                                     5.001150
## 3                                     4.944991
## 4                                     4.985838
## 5                                     5.063119
## 6                                     4.991097
##   beta.Generation_Fruit_s_Treatment0_GF_Cranberry
## 1                                        4.819816
## 2                                        4.828813
## 3                                        4.860688
## 4                                        4.806606
## 5                                        4.853945
## 6                                        4.828560
##   beta.Generation_Fruit_s_Treatment0_GF_Strawberry
## 1                                         4.698800
## 2                                         4.687128
## 3                                         4.671117
## 4                                         4.680238
## 5                                         4.729294
## 6                                         4.669222
##   beta.Generation_Fruit_s_Treatment7_Cherry_Cherry
## 1                                        0.2698853
## 2                                        0.2075909
## 3                                        0.4297513
## 4                                        0.3316380
## 5                                        0.2289434
## 6                                        0.2425714
##   beta.Generation_Fruit_s_Treatment7_Cherry_Cranberry
## 1                                           0.3024620
## 2                                           0.1762346
## 3                                           0.2720822
## 4                                           0.3754494
## 5                                           0.2150209
## 6                                           0.3136414
##   beta.Generation_Fruit_s_Treatment7_Cherry_Strawberry
## 1                                            0.4866014
## 2                                            0.4785324
## 3                                            0.5363836
## 4                                            0.4658433
## 5                                            0.5523293
## 6                                            0.4717353
##   beta.Generation_Fruit_s_Treatment7_Cranberry_Cherry
## 1                                           0.1728936
## 2                                           0.1826684
## 3                                           0.2439969
## 4                                           0.2643595
## 5                                           0.1280866
## 6                                           0.3434372
##   beta.Generation_Fruit_s_Treatment7_Cranberry_Cranberry
## 1                                              0.3177019
## 2                                              0.3241932
## 3                                              0.3375679
## 4                                              0.3647346
## 5                                              0.3702228
## 6                                              0.2935474
##   beta.Generation_Fruit_s_Treatment7_Cranberry_Strawberry
## 1                                               0.2570153
## 2                                               0.2304509
## 3                                               0.3645957
## 4                                               0.2341865
## 5                                               0.1768198
## 6                                               0.2830625
##   beta.Generation_Fruit_s_Treatment7_Strawberry_Cherry
## 1                                            0.0786434
## 2                                            0.2647675
## 3                                            0.2247322
## 4                                            0.2715666
## 5                                            0.1826883
## 6                                            0.1968905
##   beta.Generation_Fruit_s_Treatment7_Strawberry_Cranberry
## 1                                               0.4600726
## 2                                               0.4213450
## 3                                               0.3919818
## 4                                               0.5258247
## 5                                               0.4273354
## 6                                               0.5332697
##   beta.Generation_Fruit_s_Treatment7_Strawberry_Strawberry
## 1                                                0.4403463
## 2                                                0.5088651
## 3                                                0.4936399
## 4                                                0.5076107
## 5                                                0.4745148
## 6                                                0.4737480
##   beta.Generation_Fruit_s_Treatment29_Cherry_Cherry
## 1                                        -0.4610163
## 2                                        -0.4168134
## 3                                        -0.3208788
## 4                                        -0.3941687
## 5                                        -0.3797401
## 6                                        -0.3397377
##   beta.Generation_Fruit_s_Treatment29_Cherry_Cranberry
## 1                                          0.002602066
## 2                                          0.040649991
## 3                                         -0.022992731
## 4                                         -0.014541478
## 5                                          0.013119918
## 6                                         -0.011699760
##   beta.Generation_Fruit_s_Treatment29_Cherry_Strawberry
## 1                                             0.2579438
## 2                                             0.2197259
## 3                                             0.2019047
## 4                                             0.2870659
## 5                                             0.1014584
## 6                                             0.1231541
##   beta.Generation_Fruit_s_Treatment29_Cranberry_Cherry
## 1                                           -0.5004526
## 2                                           -0.2905894
## 3                                           -0.2310120
## 4                                           -0.2688588
## 5                                           -0.3517407
## 6                                           -0.2753697
##   beta.Generation_Fruit_s_Treatment29_Cranberry_Cranberry
## 1                                            -0.041562318
## 2                                            -0.023661701
## 3                                            -0.041060016
## 4                                             0.035915827
## 5                                             0.025256894
## 6                                             0.009501766
##   beta.Generation_Fruit_s_Treatment29_Cranberry_Strawberry
## 1                                               0.12741030
## 2                                               0.10265153
## 3                                               0.16281625
## 4                                               0.08162794
## 5                                               0.10300516
## 6                                               0.14537835
##   beta.Generation_Fruit_s_Treatment29_Strawberry_Cherry
## 1                                            -0.5533592
## 2                                            -0.4818896
## 3                                            -0.3104549
## 4                                            -0.4255933
## 5                                            -0.5707660
## 6                                            -0.4625740
##   beta.Generation_Fruit_s_Treatment29_Strawberry_Cranberry
## 1                                              -0.12269162
## 2                                              -0.11426361
## 3                                              -0.15134024
## 4                                              -0.07129806
## 5                                              -0.19182030
## 6                                              -0.17466625
##   beta.Generation_Fruit_s_Treatment29_Strawberry_Strawberry   sigma.Line
## 1                                                0.13123641 0.0006952132
## 2                                                0.12048085 0.0104045016
## 3                                                0.20328145 0.0070225498
## 4                                                0.09899855 0.0106039145
## 5                                                0.11590758 0.0006661946
## 6                                                0.22839030 0.0030376152
## Extract CI
bCI_tab(boo01)
##                                                                Estimate
## beta.Generation_Fruit_s_Treatment0_GF_Cherry               5.007230e+00
## beta.Generation_Fruit_s_Treatment0_GF_Cranberry            4.810966e+00
## beta.Generation_Fruit_s_Treatment0_GF_Strawberry           4.680649e+00
## beta.Generation_Fruit_s_Treatment7_Cherry_Cherry           2.688127e-01
## beta.Generation_Fruit_s_Treatment7_Cherry_Cranberry        3.536150e-01
## beta.Generation_Fruit_s_Treatment7_Cherry_Strawberry       4.837138e-01
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cherry        2.167796e-01
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cranberry     3.884097e-01
## beta.Generation_Fruit_s_Treatment7_Cranberry_Strawberry    3.074533e-01
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cherry       2.020746e-01
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cranberry    4.575152e-01
## beta.Generation_Fruit_s_Treatment7_Strawberry_Strawberry   4.812294e-01
## beta.Generation_Fruit_s_Treatment29_Cherry_Cherry         -3.650875e-01
## beta.Generation_Fruit_s_Treatment29_Cherry_Cranberry       1.574628e-02
## beta.Generation_Fruit_s_Treatment29_Cherry_Strawberry      1.961566e-01
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cherry      -3.045093e-01
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cranberry    6.893562e-03
## beta.Generation_Fruit_s_Treatment29_Cranberry_Strawberry   1.426389e-01
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cherry     -4.733171e-01
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cranberry  -1.255473e-01
## beta.Generation_Fruit_s_Treatment29_Strawberry_Strawberry  1.647607e-01
## sigma.Line                                                 1.530717e-06
##                                                                  X2.5..
## beta.Generation_Fruit_s_Treatment0_GF_Cherry               4.947951e+00
## beta.Generation_Fruit_s_Treatment0_GF_Cranberry            4.751041e+00
## beta.Generation_Fruit_s_Treatment0_GF_Strawberry           4.617101e+00
## beta.Generation_Fruit_s_Treatment7_Cherry_Cherry           1.524759e-01
## beta.Generation_Fruit_s_Treatment7_Cherry_Cranberry        1.879989e-01
## beta.Generation_Fruit_s_Treatment7_Cherry_Strawberry       3.592210e-01
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cherry        9.597523e-02
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cranberry     2.609444e-01
## beta.Generation_Fruit_s_Treatment7_Cranberry_Strawberry    9.222560e-02
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cherry       8.750623e-02
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cranberry    3.454663e-01
## beta.Generation_Fruit_s_Treatment7_Strawberry_Strawberry   3.394911e-01
## beta.Generation_Fruit_s_Treatment29_Cherry_Cherry         -4.575049e-01
## beta.Generation_Fruit_s_Treatment29_Cherry_Cranberry      -9.905060e-02
## beta.Generation_Fruit_s_Treatment29_Cherry_Strawberry      1.063414e-01
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cherry      -4.700836e-01
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cranberry   -8.865157e-02
## beta.Generation_Fruit_s_Treatment29_Cranberry_Strawberry   3.509046e-02
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cherry     -5.878579e-01
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cranberry  -2.192347e-01
## beta.Generation_Fruit_s_Treatment29_Strawberry_Strawberry  8.196792e-02
## sigma.Line                                                 1.591909e-07
##                                                                X97.5..
## beta.Generation_Fruit_s_Treatment0_GF_Cherry               5.098040365
## beta.Generation_Fruit_s_Treatment0_GF_Cranberry            4.870099926
## beta.Generation_Fruit_s_Treatment0_GF_Strawberry           4.754038740
## beta.Generation_Fruit_s_Treatment7_Cherry_Cherry           0.417472891
## beta.Generation_Fruit_s_Treatment7_Cherry_Cranberry        0.518039463
## beta.Generation_Fruit_s_Treatment7_Cherry_Strawberry       0.613574392
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cherry        0.340297232
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cranberry     0.509452446
## beta.Generation_Fruit_s_Treatment7_Cranberry_Strawberry    0.484470016
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cherry       0.338387856
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cranberry    0.588625976
## beta.Generation_Fruit_s_Treatment7_Strawberry_Strawberry   0.586513897
## beta.Generation_Fruit_s_Treatment29_Cherry_Cherry         -0.261868217
## beta.Generation_Fruit_s_Treatment29_Cherry_Cranberry       0.087462105
## beta.Generation_Fruit_s_Treatment29_Cherry_Strawberry      0.284127914
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cherry      -0.233208907
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cranberry    0.053764665
## beta.Generation_Fruit_s_Treatment29_Cranberry_Strawberry   0.208085082
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cherry     -0.322942925
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cranberry  -0.005838017
## beta.Generation_Fruit_s_Treatment29_Strawberry_Strawberry  0.280833259
## sigma.Line                                                 0.019433459
## G0: no difference among different fruit media (beta.TreatmentrelCherry / beta.TreatmentrelCranberry include zero)
## G7: no fitness change for strawberry, cherry or cranberry populations

## G29: fitness increase for cherry populations on cherry and cranberry medium, but not on strawberry medium

## G29: fitness increase for cranberry populations on cranberry and cherry media, and fitness decrease on strawberry medium

## G29: strawberry population with no change on strawberry or cranberry medium and fitness decrease on cherry medium

3.6 Analysis egg-to-adult viability

3.6.1 Compare changes in egg-to-adult viability

head(data)
##     Generation Treatment Line Fruit_s Nb_eggs Nb_adults SA Emergence_rate Obs
## 993          0    Cherry  Anc      GF      76         6  0     0.07894737   1
## 994          0    Cherry  Anc      GF      89        17  0     0.19101124   2
## 995          0    Cherry  Anc      GF      57        12  0     0.21052632   3
## 996          0    Cherry  Anc      GF     172        24  0     0.13953488   4
## 997          0    Cherry  Anc      GF     173        33  0     0.19075145   5
## 998          0    Cherry  Anc      GF      91        18  0     0.19780220   6
##     Generation_Fruit_s_Treatment Line_Treatment Treatmentrel
## 993                  0_GF_Cherry     Anc_Cherry       Cherry
## 994                  0_GF_Cherry     Anc_Cherry       Cherry
## 995                  0_GF_Cherry     Anc_Cherry       Cherry
## 996                  0_GF_Cherry     Anc_Cherry       Cherry
## 997                  0_GF_Cherry     Anc_Cherry       Cherry
## 998                  0_GF_Cherry     Anc_Cherry       Cherry
m0 <- glm(cbind(Nb_adults, Nb_eggs) ~ Generation*Fruit_s, data=data, family="binomial")
summary(m0)
## 
## Call:
## glm(formula = cbind(Nb_adults, Nb_eggs) ~ Generation * Fruit_s, 
##     family = "binomial", data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -10.9886   -2.7082    0.0541    2.3393   10.8108  
## 
## Coefficients: (5 not defined because of singularities)
##                                Estimate Std. Error  z value Pr(>|z|)    
## (Intercept)                    -1.56150    0.01232 -126.795  < 2e-16 ***
## Generation29                    0.06881    0.01789    3.847 0.000120 ***
## Generation7                    -0.36422    0.02543  -14.324  < 2e-16 ***
## Fruit_sCranberry                0.15561    0.02913    5.341 9.24e-08 ***
## Fruit_sGF                            NA         NA       NA       NA    
## Fruit_sStrawberry               0.38572    0.02574   14.984  < 2e-16 ***
## Generation29:Fruit_sCranberry  -0.11918    0.03340   -3.568 0.000359 ***
## Generation7:Fruit_sCranberry         NA         NA       NA       NA    
## Generation29:Fruit_sGF               NA         NA       NA       NA    
## Generation7:Fruit_sGF                NA         NA       NA       NA    
## Generation29:Fruit_sStrawberry -0.09466    0.03126   -3.028 0.002461 ** 
## Generation7:Fruit_sStrawberry        NA         NA       NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 22957  on 1673  degrees of freedom
## Residual deviance: 21701  on 1667  degrees of freedom
## AIC: 29527
## 
## Number of Fisher Scoring iterations: 4
m1 <- glm(cbind(Nb_adults, Nb_eggs) ~ -1 + Generation_Fruit_s_Treatment, data=data, family="binomial")
summary(m1)
## 
## Call:
## glm(formula = cbind(Nb_adults, Nb_eggs) ~ -1 + Generation_Fruit_s_Treatment, 
##     family = "binomial", data = data)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -10.7379   -2.3979    0.0021    2.1329   13.4566  
## 
## Coefficients:
##                                                      Estimate Std. Error
## Generation_Fruit_s_Treatment0_GF_Cherry              -1.78875    0.02161
## Generation_Fruit_s_Treatment0_GF_Cranberry           -1.54711    0.02154
## Generation_Fruit_s_Treatment0_GF_Strawberry          -1.32619    0.02102
## Generation_Fruit_s_Treatment29_Cherry_Cherry         -1.16777    0.02124
## Generation_Fruit_s_Treatment29_Cherry_Cranberry      -1.88638    0.02600
## Generation_Fruit_s_Treatment29_Cherry_Strawberry     -1.48678    0.02143
## Generation_Fruit_s_Treatment29_Cranberry_Cherry      -1.25528    0.01651
## Generation_Fruit_s_Treatment29_Cranberry_Cranberry   -1.34610    0.01615
## Generation_Fruit_s_Treatment29_Cranberry_Strawberry  -1.83190    0.01971
## Generation_Fruit_s_Treatment29_Strawberry_Cherry     -1.25217    0.02317
## Generation_Fruit_s_Treatment29_Strawberry_Cranberry  -1.26587    0.02159
## Generation_Fruit_s_Treatment29_Strawberry_Strawberry -1.11624    0.01882
## Generation_Fruit_s_Treatment7_Cherry_Cherry          -2.08248    0.03797
## Generation_Fruit_s_Treatment7_Cherry_Cranberry       -2.03845    0.04209
## Generation_Fruit_s_Treatment7_Cherry_Strawberry      -1.66114    0.03642
## Generation_Fruit_s_Treatment7_Cranberry_Cherry       -2.04718    0.03722
## Generation_Fruit_s_Treatment7_Cranberry_Cranberry    -1.81037    0.02987
## Generation_Fruit_s_Treatment7_Cranberry_Strawberry   -1.46342    0.03220
## Generation_Fruit_s_Treatment7_Strawberry_Cherry      -1.49135    0.02208
## Generation_Fruit_s_Treatment7_Strawberry_Cranberry   -1.70626    0.02404
## Generation_Fruit_s_Treatment7_Strawberry_Strawberry  -1.44195    0.02147
##                                                      z value Pr(>|z|)    
## Generation_Fruit_s_Treatment0_GF_Cherry               -82.77   <2e-16 ***
## Generation_Fruit_s_Treatment0_GF_Cranberry            -71.84   <2e-16 ***
## Generation_Fruit_s_Treatment0_GF_Strawberry           -63.08   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Cherry          -54.97   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Cranberry       -72.54   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Strawberry      -69.38   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Cherry       -76.02   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Cranberry    -83.33   <2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Strawberry   -92.95   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Cherry      -54.05   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Cranberry   -58.63   <2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Strawberry  -59.31   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Cherry           -54.84   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Cranberry        -48.43   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Strawberry       -45.61   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Cherry        -55.00   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Cranberry     -60.60   <2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Strawberry    -45.45   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Cherry       -67.53   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Cranberry    -70.97   <2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Strawberry   -67.16   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 142163  on 1674  degrees of freedom
## Residual deviance:  20076  on 1653  degrees of freedom
## AIC: 27930
## 
## Number of Fisher Scoring iterations: 5
m2 <- lme4::glmer(cbind(Nb_adults, Nb_eggs) ~ -1 + Generation_Fruit_s_Treatment + (1|Line), data = data, verbose=TRUE, family="binomial")
## start par. =  1 fn =  27096.05 
## At return
## eval:  23 fn:      27033.634 par: 0.180886
## (NM) 20: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 40: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 60: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 80: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 100: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 120: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 140: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 160: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 180: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 200: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 220: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 240: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 260: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 280: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 300: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 320: f = 27033.6 at 0.180886 -1.78875 -1.54711 -1.32619 -1.17115 -1.89452 -1.49178 -1.25176 -1.35733   -1.843 -1.26965 -1.29259 -1.14724  -2.0963 -2.07523  -1.7086 -1.89216 -1.69028 -1.30098 -1.55205  -1.7655 -1.49762
## (NM) 340: f = 27033.6 at 0.180987 -1.78849 -1.54692 -1.32605 -1.17115 -1.89458 -1.49174 -1.25174 -1.35728 -1.84295  -1.2696 -1.29249 -1.14723 -2.09669 -2.07553 -1.70901 -1.89256 -1.69057 -1.30124 -1.55195 -1.76546 -1.49751
## (NM) 360: f = 27033.6 at 0.181001 -1.78854 -1.54698  -1.3261 -1.17118  -1.8946 -1.49177 -1.25178 -1.35732 -1.84299 -1.26961 -1.29249 -1.14724 -2.09675 -2.07558 -1.70907 -1.89259 -1.69062 -1.30127 -1.55199 -1.76549 -1.49755
## (NM) 380: f = 27033.6 at 0.180897 -1.78898 -1.54736  -1.3264 -1.17146 -1.89491 -1.49211 -1.25202 -1.35761  -1.8433 -1.26966 -1.29274 -1.14743 -2.09713 -2.07591 -1.70941  -1.8927 -1.69087 -1.30143 -1.55228 -1.76566 -1.49784
## (NM) 400: f = 27033.6 at 0.180912 -1.78907 -1.54743  -1.3265 -1.17151 -1.89487 -1.49213 -1.25203 -1.35766 -1.84332 -1.26991 -1.29277 -1.14743 -2.09718 -2.07611  -1.7094 -1.89278  -1.6909 -1.30152 -1.55245 -1.76596 -1.49787
## (NM) 420: f = 27033.6 at 0.180934 -1.78917 -1.54756 -1.32662 -1.17159 -1.89497 -1.49221  -1.2521 -1.35773 -1.84338 -1.26997 -1.29284 -1.14751 -2.09727 -2.07623 -1.70952 -1.89289   -1.691 -1.30162 -1.55253 -1.76606 -1.49797
## (NM) 440: f = 27033.6 at 0.180828 -1.78902 -1.54735 -1.32651 -1.17143 -1.89481 -1.49209 -1.25199 -1.35758 -1.84326 -1.26984 -1.29276  -1.1474 -2.09712 -2.07599 -1.70937 -1.89279 -1.69088 -1.30147 -1.55228 -1.76578 -1.49789
## (NM) 460: f = 27033.6 at 0.180839 -1.78919 -1.54755 -1.32659 -1.17151 -1.89501  -1.4922 -1.25212 -1.35767 -1.84336 -1.26986 -1.29284 -1.14747 -2.09727 -2.07623 -1.70949 -1.89301 -1.69105 -1.30163 -1.55251 -1.76596 -1.49803
## (NM) 480: f = 27033.6 at 0.180889 -1.78913 -1.54752  -1.3266 -1.17146 -1.89487  -1.4921 -1.25206  -1.3576 -1.84333 -1.26982 -1.29275 -1.14742 -2.09723 -2.07615 -1.70941 -1.89289   -1.691 -1.30156 -1.55249 -1.76588 -1.49804
## (NM) 500: f = 27033.6 at 0.180855 -1.78909 -1.54744 -1.32654 -1.17147 -1.89487 -1.49212 -1.25202 -1.35761  -1.8433 -1.26986 -1.29278 -1.14743 -2.09717 -2.07608  -1.7094 -1.89285 -1.69094 -1.30152 -1.55236 -1.76584 -1.49794
## (NM) 520: f = 27033.6 at 0.180857 -1.78917 -1.54754 -1.32658 -1.17151 -1.89497 -1.49218 -1.25209 -1.35766 -1.84335 -1.26987 -1.29283 -1.14746 -2.09726  -2.0762 -1.70947 -1.89296 -1.69103 -1.30161 -1.55247 -1.76594 -1.49801
## (NM) 540: f = 27033.6 at  0.18087 -1.78919 -1.54755 -1.32661  -1.1715 -1.89493 -1.49217 -1.25209 -1.35766 -1.84336 -1.26992 -1.29284  -1.1475 -2.09727  -2.0762 -1.70944 -1.89291 -1.69103 -1.30161 -1.55247 -1.76594 -1.49801
## (NM) 560: f = 27033.6 at  0.18088 -1.78915 -1.54753  -1.3266 -1.17148  -1.8949 -1.49213 -1.25206 -1.35762 -1.84334 -1.26985 -1.29278 -1.14743 -2.09723 -2.07616 -1.70942  -1.8929   -1.691 -1.30158 -1.55247  -1.7659 -1.49802
## (NM) 580: f = 27033.6 at 0.180879 -1.78921 -1.54753 -1.32662 -1.17147 -1.89485 -1.49211 -1.25203 -1.35762 -1.84334 -1.26987  -1.2928 -1.14743 -2.09722 -2.07615 -1.70941 -1.89289 -1.69101  -1.3016 -1.55246 -1.76593 -1.49804
## (NM) 600: f = 27033.6 at 0.180879 -1.78921 -1.54755 -1.32663 -1.17149  -1.8949 -1.49215 -1.25207 -1.35764 -1.84336 -1.26987 -1.29281 -1.14746 -2.09726  -2.0762 -1.70946 -1.89293 -1.69102 -1.30162 -1.55252 -1.76599 -1.49805
## (NM) 620: f = 27033.6 at 0.180881  -1.7892 -1.54754 -1.32663 -1.17146 -1.89488 -1.49213 -1.25205 -1.35762 -1.84334 -1.26986 -1.29279 -1.14743 -2.09721 -2.07615 -1.70943 -1.89293 -1.69101 -1.30161 -1.55249 -1.76596 -1.49806
## (NM) 640: f = 27033.6 at 0.180885 -1.78922 -1.54754 -1.32663 -1.17146 -1.89487 -1.49213 -1.25204 -1.35761 -1.84334 -1.26985 -1.29279 -1.14741 -2.09721 -2.07615 -1.70942 -1.89292 -1.69101 -1.30162  -1.5525 -1.76599 -1.49808
## (NM) 660: f = 27033.6 at 0.180891 -1.78919 -1.54755 -1.32663 -1.17147 -1.89487 -1.49212 -1.25204 -1.35762 -1.84334 -1.26984 -1.29278 -1.14742 -2.09723 -2.07617 -1.70945 -1.89292 -1.69102 -1.30162  -1.5525 -1.76598 -1.49807
## (NM) 680: f = 27033.6 at 0.180891 -1.78918 -1.54754 -1.32663 -1.17143 -1.89484 -1.49209 -1.25202  -1.3576 -1.84332 -1.26984 -1.29277 -1.14739  -2.0972 -2.07614 -1.70942 -1.89293 -1.69099 -1.30163 -1.55252   -1.766 -1.49809
## (NM) 700: f = 27033.6 at 0.180898 -1.78919 -1.54756 -1.32663 -1.17143 -1.89484 -1.49209 -1.25202  -1.3576 -1.84332 -1.26983 -1.29277 -1.14741 -2.09723 -2.07617 -1.70945 -1.89294 -1.69102 -1.30163 -1.55255 -1.76601 -1.49811
## (NM) 720: f = 27033.6 at 0.180887 -1.78915 -1.54751 -1.32659 -1.17138 -1.89478 -1.49204 -1.25197 -1.35755 -1.84327 -1.26978 -1.29272 -1.14734 -2.09718 -2.07612 -1.70938 -1.89293 -1.69101 -1.30162 -1.55256 -1.76602 -1.49811
## (NM) 740: f = 27033.6 at  0.18088  -1.7892 -1.54758 -1.32664 -1.17139 -1.89482 -1.49205 -1.25199 -1.35758 -1.84329 -1.26982 -1.29274 -1.14738 -2.09723 -2.07616 -1.70944 -1.89299 -1.69108 -1.30169 -1.55264 -1.76612 -1.49818
## (NM) 760: f = 27033.6 at 0.180895 -1.78919 -1.54756 -1.32664 -1.17133 -1.89478   -1.492 -1.25197 -1.35755 -1.84325 -1.26978 -1.29272 -1.14735  -2.0972 -2.07612 -1.70939 -1.89295 -1.69104 -1.30167 -1.55264 -1.76611  -1.4982
## (NM) 780: f = 27033.6 at 0.180891 -1.78918 -1.54755 -1.32662 -1.17131 -1.89474 -1.49198 -1.25194 -1.35752 -1.84324 -1.26973 -1.29266  -1.1473 -2.09717  -2.0761 -1.70939 -1.89297 -1.69106 -1.30167 -1.55265 -1.76611 -1.49822
## (NM) 800: f = 27033.6 at 0.180887 -1.78917 -1.54754 -1.32661 -1.17129 -1.89472 -1.49195 -1.25192  -1.3575 -1.84322 -1.26971 -1.29264 -1.14728 -2.09716 -2.07608 -1.70937 -1.89297 -1.69107 -1.30168 -1.55267 -1.76612 -1.49823
## (NM) 820: f = 27033.6 at 0.180872 -1.78916 -1.54752  -1.3266 -1.17128 -1.89472 -1.49193 -1.25192 -1.35749  -1.8432 -1.26969 -1.29263 -1.14727 -2.09718 -2.07609 -1.70937 -1.89296 -1.69107 -1.30167 -1.55269 -1.76616 -1.49825
## (NM) 840: f = 27033.6 at 0.180872 -1.78916 -1.54752  -1.3266 -1.17128 -1.89472 -1.49193 -1.25192 -1.35749  -1.8432 -1.26969 -1.29263 -1.14727 -2.09718 -2.07609 -1.70937 -1.89296 -1.69107 -1.30167 -1.55269 -1.76616 -1.49825
## (NM) 860: f = 27033.6 at 0.180878 -1.78919 -1.54754 -1.32661  -1.1713 -1.89474 -1.49196 -1.25193 -1.35751 -1.84322 -1.26971 -1.29265 -1.14728 -2.09719 -2.07613 -1.70941 -1.89298 -1.69109 -1.30169 -1.55269 -1.76615 -1.49825
## (NM) 880: f = 27033.6 at 0.180875 -1.78916 -1.54752 -1.32659 -1.17127  -1.8947 -1.49193 -1.25191 -1.35748  -1.8432 -1.26971 -1.29263 -1.14727 -2.09718 -2.07611 -1.70938 -1.89298 -1.69107 -1.30168 -1.55269 -1.76616 -1.49824
## (NM) 900: f = 27033.6 at 0.180871 -1.78919 -1.54753 -1.32661 -1.17131 -1.89472 -1.49195 -1.25193  -1.3575 -1.84322 -1.26971 -1.29264 -1.14729 -2.09721 -2.07615 -1.70941 -1.89298 -1.69108 -1.30169 -1.55269 -1.76617 -1.49826
## (NM) 920: f = 27033.6 at 0.180875 -1.78918 -1.54754 -1.32662  -1.1713 -1.89472 -1.49196 -1.25193 -1.35751 -1.84322 -1.26972 -1.29265 -1.14729 -2.09722 -2.07616 -1.70944 -1.89299 -1.69109  -1.3017 -1.55271 -1.76617 -1.49827
## (NM) 940: f = 27033.6 at 0.180868 -1.78917 -1.54752 -1.32661 -1.17129 -1.89471 -1.49194 -1.25191 -1.35749  -1.8432  -1.2697 -1.29263 -1.14728 -2.09722 -2.07615 -1.70942 -1.89299 -1.69108 -1.30169 -1.55269 -1.76618 -1.49826
## (NM) 960: f = 27033.6 at 0.180874 -1.78917 -1.54752 -1.32659 -1.17128  -1.8947 -1.49193 -1.25191 -1.35749  -1.8432  -1.2697 -1.29263 -1.14727  -2.0972 -2.07613  -1.7094 -1.89298 -1.69107 -1.30168 -1.55269 -1.76617 -1.49825
## (NM) 980: f = 27033.6 at 0.180875 -1.78917 -1.54752  -1.3266 -1.17129 -1.89471 -1.49194 -1.25192 -1.35749 -1.84321  -1.2697 -1.29263 -1.14728 -2.09721 -2.07614 -1.70942 -1.89297 -1.69107 -1.30167 -1.55269 -1.76617 -1.49826
## (NM) 1000: f = 27033.6 at 0.180872 -1.78916 -1.54752  -1.3266 -1.17129 -1.89471 -1.49195 -1.25191 -1.35749  -1.8432  -1.2697 -1.29263 -1.14728 -2.09722 -2.07615 -1.70943 -1.89298 -1.69107 -1.30168 -1.55269 -1.76618 -1.49825
## (NM) 1020: f = 27033.6 at 0.180876 -1.78916 -1.54751 -1.32659 -1.17128  -1.8947 -1.49193 -1.25191 -1.35749  -1.8432 -1.26968 -1.29261 -1.14726 -2.09723 -2.07615 -1.70943 -1.89299 -1.69108 -1.30168  -1.5527 -1.76619 -1.49827
## (NM) 1040: f = 27033.6 at 0.180876 -1.78916 -1.54751 -1.32659 -1.17128  -1.8947 -1.49193 -1.25191 -1.35749  -1.8432 -1.26968 -1.29261 -1.14726 -2.09723 -2.07615 -1.70943 -1.89299 -1.69108 -1.30168  -1.5527 -1.76619 -1.49827
## (NM) 1060: f = 27033.6 at 0.180875 -1.78915  -1.5475 -1.32658 -1.17127 -1.89469 -1.49193  -1.2519 -1.35748 -1.84319 -1.26967  -1.2926 -1.14725 -2.09724 -2.07615 -1.70944 -1.89298 -1.69107 -1.30167  -1.5527 -1.76619 -1.49827
## (NM) 1080: f = 27033.6 at 0.180878 -1.78916 -1.54752 -1.32659 -1.17127  -1.8947 -1.49193  -1.2519 -1.35748 -1.84319 -1.26968 -1.29261 -1.14726 -2.09723 -2.07615 -1.70944 -1.89298 -1.69108 -1.30168 -1.55271 -1.76619 -1.49828
## (NM) 1100: f = 27033.6 at 0.180876 -1.78916 -1.54751 -1.32658 -1.17128 -1.89471 -1.49195 -1.25191 -1.35749  -1.8432 -1.26969 -1.29262 -1.14727 -2.09725 -2.07616 -1.70945 -1.89298 -1.69107 -1.30167 -1.55269 -1.76618 -1.49826
## (NM) 1120: f = 27033.6 at 0.180876 -1.78915 -1.54751 -1.32658 -1.17128  -1.8947 -1.49194 -1.25191 -1.35749  -1.8432 -1.26968 -1.29261 -1.14726 -2.09725 -2.07617 -1.70946 -1.89298 -1.69107 -1.30168  -1.5527 -1.76619 -1.49827
## (NM) 1140: f = 27033.6 at 0.180875 -1.78915 -1.54751 -1.32658 -1.17128  -1.8947 -1.49194 -1.25191 -1.35749  -1.8432 -1.26968 -1.29262 -1.14726 -2.09725 -2.07617 -1.70945 -1.89298 -1.69107 -1.30167 -1.55271 -1.76619 -1.49827
## (NM) 1160: f = 27033.6 at 0.180878 -1.78914 -1.54749 -1.32657 -1.17127 -1.89469 -1.49193  -1.2519 -1.35748 -1.84319 -1.26968 -1.29261 -1.14726 -2.09725 -2.07616 -1.70946 -1.89297 -1.69106 -1.30166  -1.5527 -1.76619 -1.49827
## (NM) 1180: f = 27033.6 at 0.180876 -1.78915  -1.5475 -1.32657 -1.17127  -1.8947 -1.49194 -1.25191 -1.35749 -1.84319 -1.26968 -1.29262 -1.14726 -2.09726 -2.07618 -1.70947 -1.89297 -1.69107 -1.30167 -1.55271 -1.76619 -1.49827
## (NM) 1200: f = 27033.6 at 0.180879 -1.78914 -1.54749 -1.32657 -1.17126 -1.89469 -1.49193  -1.2519 -1.35749 -1.84319 -1.26968 -1.29262 -1.14726 -2.09725 -2.07617 -1.70946 -1.89297 -1.69107 -1.30167 -1.55271 -1.76619 -1.49828
## (NM) 1220: f = 27033.6 at  0.18088 -1.78913 -1.54748 -1.32656 -1.17126 -1.89469 -1.49193  -1.2519 -1.35748 -1.84319 -1.26968 -1.29262 -1.14726 -2.09727 -2.07618 -1.70948 -1.89296 -1.69106 -1.30166 -1.55271 -1.76619 -1.49828
## (NM) 1240: f = 27033.6 at  0.18088 -1.78913 -1.54749 -1.32657 -1.17127 -1.89469 -1.49193  -1.2519 -1.35749 -1.84319 -1.26969 -1.29263 -1.14727 -2.09726 -2.07618 -1.70947 -1.89296 -1.69107 -1.30167 -1.55271 -1.76619 -1.49828
## (NM) 1260: f = 27033.6 at 0.180879 -1.78914 -1.54749 -1.32657 -1.17126 -1.89468 -1.49192  -1.2519 -1.35748 -1.84319 -1.26969 -1.29262 -1.14727 -2.09725 -2.07619 -1.70947 -1.89297 -1.69107 -1.30167 -1.55272  -1.7662 -1.49829
## (NM) 1280: f = 27033.6 at  0.18088 -1.78911 -1.54747 -1.32655 -1.17124 -1.89467 -1.49191 -1.25189 -1.35748 -1.84318 -1.26969 -1.29263 -1.14727 -2.09726 -2.07619 -1.70948 -1.89295 -1.69107 -1.30166 -1.55272  -1.7662 -1.49829
## (NM) 1300: f = 27033.6 at 0.180877 -1.78912 -1.54748 -1.32655 -1.17125 -1.89468 -1.49192  -1.2519 -1.35748 -1.84319  -1.2697 -1.29264 -1.14728 -2.09726 -2.07619 -1.70948 -1.89295 -1.69106 -1.30165 -1.55272  -1.7662 -1.49828
## (NM) 1320: f = 27033.6 at 0.180877 -1.78909 -1.54745 -1.32652 -1.17124 -1.89466  -1.4919 -1.25189 -1.35748 -1.84318  -1.2697 -1.29264 -1.14728 -2.09728  -2.0762 -1.70948 -1.89294 -1.69105 -1.30164 -1.55271 -1.76618 -1.49827
## (NM) 1340: f = 27033.6 at 0.180873 -1.78908 -1.54744 -1.32651 -1.17124 -1.89466  -1.4919 -1.25189 -1.35748 -1.84318  -1.2697 -1.29264 -1.14728 -2.09728 -2.07622  -1.7095 -1.89294 -1.69104 -1.30163 -1.55272 -1.76619 -1.49828
## (NM) 1360: f = 27033.6 at 0.180869 -1.78906 -1.54742  -1.3265 -1.17123 -1.89465 -1.49189 -1.25189 -1.35747 -1.84318 -1.26971 -1.29265 -1.14729 -2.09729 -2.07623 -1.70949 -1.89293 -1.69104 -1.30162 -1.55272 -1.76619 -1.49828
## (NM) 1380: f = 27033.6 at 0.180872 -1.78907 -1.54743  -1.3265 -1.17125 -1.89467 -1.49191  -1.2519 -1.35748 -1.84319 -1.26971 -1.29265 -1.14729  -2.0973 -2.07623  -1.7095 -1.89294 -1.69104 -1.30163 -1.55271 -1.76618 -1.49827
## (NM) 1400: f = 27033.6 at 0.180875 -1.78907 -1.54743 -1.32651 -1.17123 -1.89465 -1.49189 -1.25189 -1.35747 -1.84318 -1.26971 -1.29265 -1.14729 -2.09728 -2.07622 -1.70949 -1.89293 -1.69104 -1.30163 -1.55272  -1.7662 -1.49828
## (NM) 1420: f = 27033.6 at 0.180874 -1.78908 -1.54744 -1.32651 -1.17123 -1.89466 -1.49189 -1.25189 -1.35747 -1.84318 -1.26971 -1.29265 -1.14728 -2.09728 -2.07621 -1.70949 -1.89294 -1.69104 -1.30163 -1.55271 -1.76619 -1.49828
## (NM) 1440: f = 27033.6 at 0.180874 -1.78907 -1.54742  -1.3265 -1.17123 -1.89465 -1.49189 -1.25189 -1.35747 -1.84318 -1.26971 -1.29265 -1.14729 -2.09729 -2.07623  -1.7095 -1.89293 -1.69103 -1.30162 -1.55271 -1.76619 -1.49827
## (NM) 1460: f = 27033.6 at 0.180874 -1.78908 -1.54743 -1.32651 -1.17124 -1.89466  -1.4919  -1.2519 -1.35748 -1.84319 -1.26971 -1.29265 -1.14729 -2.09729 -2.07622 -1.70949 -1.89294 -1.69104 -1.30163 -1.55271 -1.76619 -1.49827
## (NM) 1480: f = 27033.6 at 0.180877 -1.78908 -1.54744 -1.32651 -1.17124 -1.89466  -1.4919  -1.2519 -1.35748 -1.84319 -1.26972 -1.29266  -1.1473 -2.09728 -2.07622 -1.70949 -1.89293 -1.69104 -1.30163 -1.55271 -1.76619 -1.49828
## (NM) 1500: f = 27033.6 at 0.180878 -1.78907 -1.54743  -1.3265 -1.17123 -1.89465 -1.49189  -1.2519 -1.35748 -1.84319 -1.26973 -1.29267  -1.1473 -2.09729 -2.07623 -1.70949 -1.89293 -1.69104 -1.30163 -1.55271 -1.76619 -1.49827
## (NM) 1520: f = 27033.6 at 0.180879 -1.78907 -1.54743  -1.3265 -1.17123 -1.89465 -1.49189  -1.2519 -1.35748 -1.84319 -1.26973 -1.29266  -1.1473 -2.09728 -2.07622 -1.70949 -1.89293 -1.69104 -1.30163 -1.55271 -1.76619 -1.49827
## (NM) 1540: f = 27033.6 at 0.180881 -1.78908 -1.54743 -1.32651 -1.17123 -1.89465 -1.49189  -1.2519 -1.35748 -1.84319 -1.26972 -1.29266  -1.1473 -2.09728 -2.07622 -1.70949 -1.89294 -1.69104 -1.30163 -1.55271 -1.76619 -1.49828
summary(m2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: cbind(Nb_adults, Nb_eggs) ~ -1 + Generation_Fruit_s_Treatment +  
##     (1 | Line)
##    Data: data
## 
##      AIC      BIC   logLik deviance df.resid 
##  27077.6  27196.9 -13516.8  27033.6     1652 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -8.2174 -2.2315  0.0267  2.2417 17.5345 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Line   (Intercept) 0.03272  0.1809  
## Number of obs: 1674, groups:  Line, 26
## 
## Fixed effects:
##                                                      Estimate Std. Error
## Generation_Fruit_s_Treatment0_GF_Cherry              -1.78908    0.18217
## Generation_Fruit_s_Treatment0_GF_Cranberry           -1.54743    0.18216
## Generation_Fruit_s_Treatment0_GF_Strawberry          -1.32651    0.18210
## Generation_Fruit_s_Treatment29_Cherry_Cherry         -1.17123    0.10658
## Generation_Fruit_s_Treatment29_Cherry_Cranberry      -1.89465    0.10764
## Generation_Fruit_s_Treatment29_Cherry_Strawberry     -1.49189    0.10662
## Generation_Fruit_s_Treatment29_Cranberry_Cherry      -1.25190    0.08256
## Generation_Fruit_s_Treatment29_Cranberry_Cranberry   -1.35748    0.08251
## Generation_Fruit_s_Treatment29_Cranberry_Strawberry  -1.84319    0.08327
## Generation_Fruit_s_Treatment29_Strawberry_Cherry     -1.26972    0.10702
## Generation_Fruit_s_Treatment29_Strawberry_Cranberry  -1.29266    0.10670
## Generation_Fruit_s_Treatment29_Strawberry_Strawberry -1.14730    0.10616
## Generation_Fruit_s_Treatment7_Cherry_Cherry          -2.09728    0.09867
## Generation_Fruit_s_Treatment7_Cherry_Cranberry       -2.07622    0.10064
## Generation_Fruit_s_Treatment7_Cherry_Strawberry      -1.70949    0.09882
## Generation_Fruit_s_Treatment7_Cranberry_Cherry       -1.89293    0.09035
## Generation_Fruit_s_Treatment7_Cranberry_Cranberry    -1.69104    0.08694
## Generation_Fruit_s_Treatment7_Cranberry_Strawberry   -1.30163    0.08831
## Generation_Fruit_s_Treatment7_Strawberry_Cherry      -1.55271    0.08592
## Generation_Fruit_s_Treatment7_Strawberry_Cranberry   -1.76619    0.08620
## Generation_Fruit_s_Treatment7_Strawberry_Strawberry  -1.49828    0.08545
##                                                      z value Pr(>|z|)    
## Generation_Fruit_s_Treatment0_GF_Cherry               -9.821  < 2e-16 ***
## Generation_Fruit_s_Treatment0_GF_Cranberry            -8.495  < 2e-16 ***
## Generation_Fruit_s_Treatment0_GF_Strawberry           -7.284 3.23e-13 ***
## Generation_Fruit_s_Treatment29_Cherry_Cherry         -10.990  < 2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Cranberry      -17.602  < 2e-16 ***
## Generation_Fruit_s_Treatment29_Cherry_Strawberry     -13.993  < 2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Cherry      -15.163  < 2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Cranberry   -16.453  < 2e-16 ***
## Generation_Fruit_s_Treatment29_Cranberry_Strawberry  -22.134  < 2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Cherry     -11.865  < 2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Cranberry  -12.115  < 2e-16 ***
## Generation_Fruit_s_Treatment29_Strawberry_Strawberry -10.807  < 2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Cherry          -21.256  < 2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Cranberry       -20.631  < 2e-16 ***
## Generation_Fruit_s_Treatment7_Cherry_Strawberry      -17.300  < 2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Cherry       -20.950  < 2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Cranberry    -19.450  < 2e-16 ***
## Generation_Fruit_s_Treatment7_Cranberry_Strawberry   -14.740  < 2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Cherry      -18.071  < 2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Cranberry   -20.490  < 2e-16 ***
## Generation_Fruit_s_Treatment7_Strawberry_Strawberry  -17.533  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
MuMIn::model.sel(m0, m1, m2)
## Model selection table 
##     (Int) Frt_s Gnr Frt_s:Gnr Gnr_Frt_s_Trt          family    class verbose
## m2                                        + binomial(logit) glmerMod    TRUE
## m1                                        + binomial(logit)      glm        
## m0 -1.561     +   +         +               binomial(logit)      glm        
##    random df    logLik    AICc   delta weight
## m2      L 22 -13516.82 27078.2    0.00      1
## m1        21 -13943.84 27930.2  851.99      0
## m0         7 -14756.38 29526.8 2448.59      0
## Models ranked by AICc(x) 
## Random terms: 
## L = '1 | Line'
## Check residuals for the different experimental populations
plot(m2, resid(.) ~ as.numeric(Line))

boo01 <- lme4::bootMer(m2, mySumm, nsim = 50, re.form = NA)

head(data.frame(boo01))
##   beta.Generation_Fruit_s_Treatment0_GF_Cherry
## 1                                    -1.743725
## 2                                    -1.865371
## 3                                    -1.851976
## 4                                    -1.724574
## 5                                    -1.756578
## 6                                    -1.817264
##   beta.Generation_Fruit_s_Treatment0_GF_Cranberry
## 1                                       -1.541776
## 2                                       -1.618555
## 3                                       -1.597629
## 4                                       -1.517730
## 5                                       -1.499889
## 6                                       -1.582096
##   beta.Generation_Fruit_s_Treatment0_GF_Strawberry
## 1                                        -1.319072
## 2                                        -1.373358
## 3                                        -1.391124
## 4                                        -1.319846
## 5                                        -1.284999
## 6                                        -1.362666
##   beta.Generation_Fruit_s_Treatment7_Cherry_Cherry
## 1                                       -0.2953520
## 2                                       -0.2689570
## 3                                       -0.2474043
## 4                                       -0.2787308
## 5                                       -0.5268370
## 6                                       -0.4000189
##   beta.Generation_Fruit_s_Treatment7_Cherry_Cranberry
## 1                                          -0.5092523
## 2                                          -0.5265649
## 3                                          -0.4691874
## 4                                          -0.4453035
## 5                                          -0.7473835
## 6                                          -0.5908984
##   beta.Generation_Fruit_s_Treatment7_Cherry_Strawberry
## 1                                           -0.3954085
## 2                                           -0.4058025
## 3                                           -0.3697334
## 4                                           -0.2345346
## 5                                           -0.6268052
## 6                                           -0.4430149
##   beta.Generation_Fruit_s_Treatment7_Cranberry_Cherry
## 1                                          -0.1976273
## 2                                           0.1641529
## 3                                          -0.0614760
## 4                                          -0.2039432
## 5                                          -0.1383276
## 6                                          -0.1386298
##   beta.Generation_Fruit_s_Treatment7_Cranberry_Cranberry
## 1                                             -0.2139803
## 2                                              0.1260060
## 3                                             -0.0541312
## 4                                             -0.2142183
## 5                                             -0.2455216
## 6                                             -0.1824070
##   beta.Generation_Fruit_s_Treatment7_Cranberry_Strawberry
## 1                                             -0.09334005
## 2                                              0.28528263
## 3                                              0.10173848
## 4                                             -0.01405573
## 5                                              0.01848418
## 6                                              0.05928137
##   beta.Generation_Fruit_s_Treatment7_Strawberry_Cherry
## 1                                            0.1488840
## 2                                            0.4409035
## 3                                            0.4516813
## 4                                            0.1379182
## 5                                            0.2564592
## 6                                            0.1627865
##   beta.Generation_Fruit_s_Treatment7_Strawberry_Cranberry
## 1                                             -0.26837873
## 2                                             -0.03155987
## 3                                             -0.05159086
## 4                                             -0.26900056
## 5                                             -0.16997340
## 6                                             -0.23538190
##   beta.Generation_Fruit_s_Treatment7_Strawberry_Strawberry
## 1                                             -0.225729710
## 2                                             -0.002394596
## 3                                              0.035139939
## 4                                             -0.189858442
## 5                                             -0.131298656
## 6                                             -0.240091554
##   beta.Generation_Fruit_s_Treatment29_Cherry_Cherry
## 1                                         0.5915223
## 2                                         0.6954692
## 3                                         0.5366219
## 4                                         0.4967005
## 5                                         0.5974800
## 6                                         0.7289909
##   beta.Generation_Fruit_s_Treatment29_Cherry_Cranberry
## 1                                           -0.3390496
## 2                                           -0.2998871
## 3                                           -0.5274240
## 4                                           -0.4534270
## 5                                           -0.3953031
## 6                                           -0.2157260
##   beta.Generation_Fruit_s_Treatment29_Cherry_Strawberry
## 1                                           -0.18574860
## 2                                           -0.11708914
## 3                                           -0.25177242
## 4                                           -0.22855351
## 5                                           -0.20126650
## 6                                           -0.07124183
##   beta.Generation_Fruit_s_Treatment29_Cranberry_Cherry
## 1                                            0.5401418
## 2                                            0.5865537
## 3                                            0.6951295
## 4                                            0.4228175
## 5                                            0.5837039
## 6                                            0.6808567
##   beta.Generation_Fruit_s_Treatment29_Cranberry_Cranberry
## 1                                               0.2625346
## 2                                               0.2272899
## 3                                               0.3474279
## 4                                               0.1292057
## 5                                               0.1853206
## 6                                               0.3435737
##   beta.Generation_Fruit_s_Treatment29_Cranberry_Strawberry
## 1                                               -0.4732706
## 2                                               -0.4625041
## 3                                               -0.3664613
## 4                                               -0.5319951
## 5                                               -0.4877154
## 6                                               -0.3482399
##   beta.Generation_Fruit_s_Treatment29_Strawberry_Cherry
## 1                                             0.5143357
## 2                                             0.4746184
## 3                                             0.6641229
## 4                                             0.4963949
## 5                                             0.5150906
## 6                                             0.6177252
##   beta.Generation_Fruit_s_Treatment29_Strawberry_Cranberry
## 1                                                0.2874851
## 2                                                0.1453642
## 3                                                0.3661118
## 4                                                0.2299310
## 5                                                0.2548419
## 6                                                0.3832180
##   beta.Generation_Fruit_s_Treatment29_Strawberry_Strawberry sigma.Line
## 1                                                0.22886508  0.1461729
## 2                                                0.04712573  0.1328527
## 3                                                0.31334466  0.1528380
## 4                                                0.21095589  0.1635870
## 5                                                0.17577524  0.1749053
## 6                                                0.29186712  0.1491875
## Extract CI
bCI_tab(boo01)
##                                                             Estimate
## beta.Generation_Fruit_s_Treatment0_GF_Cherry              -1.7890786
## beta.Generation_Fruit_s_Treatment0_GF_Cranberry           -1.5474332
## beta.Generation_Fruit_s_Treatment0_GF_Strawberry          -1.3265083
## beta.Generation_Fruit_s_Treatment7_Cherry_Cherry          -0.3082037
## beta.Generation_Fruit_s_Treatment7_Cherry_Cranberry       -0.5287878
## beta.Generation_Fruit_s_Treatment7_Cherry_Strawberry      -0.3829831
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cherry       -0.1038564
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cranberry    -0.1436100
## beta.Generation_Fruit_s_Treatment7_Cranberry_Strawberry    0.0248736
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cherry       0.2363712
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cranberry   -0.2187578
## beta.Generation_Fruit_s_Treatment7_Strawberry_Strawberry  -0.1717675
## beta.Generation_Fruit_s_Treatment29_Cherry_Cherry          0.6178465
## beta.Generation_Fruit_s_Treatment29_Cherry_Cranberry      -0.3472205
## beta.Generation_Fruit_s_Treatment29_Cherry_Strawberry     -0.1653853
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cherry       0.5371816
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cranberry    0.1899540
## beta.Generation_Fruit_s_Treatment29_Cranberry_Strawberry  -0.5166812
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cherry      0.5193550
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cranberry   0.2547695
## beta.Generation_Fruit_s_Treatment29_Strawberry_Strawberry  0.1792083
## sigma.Line                                                 0.1808808
##                                                                X2.5..
## beta.Generation_Fruit_s_Treatment0_GF_Cherry              -2.00515081
## beta.Generation_Fruit_s_Treatment0_GF_Cranberry           -1.68105789
## beta.Generation_Fruit_s_Treatment0_GF_Strawberry          -1.48081566
## beta.Generation_Fruit_s_Treatment7_Cherry_Cherry          -0.52549034
## beta.Generation_Fruit_s_Treatment7_Cherry_Cranberry       -0.82725255
## beta.Generation_Fruit_s_Treatment7_Cherry_Strawberry      -0.61556496
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cherry       -0.45928385
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cranberry    -0.50743713
## beta.Generation_Fruit_s_Treatment7_Cranberry_Strawberry   -0.32180494
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cherry      -0.13708834
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cranberry   -0.57839874
## beta.Generation_Fruit_s_Treatment7_Strawberry_Strawberry  -0.53240263
## beta.Generation_Fruit_s_Treatment29_Cherry_Cherry          0.35482159
## beta.Generation_Fruit_s_Treatment29_Cherry_Cranberry      -0.62779722
## beta.Generation_Fruit_s_Treatment29_Cherry_Strawberry     -0.42551499
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cherry       0.24666697
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cranberry   -0.07561325
## beta.Generation_Fruit_s_Treatment29_Cranberry_Strawberry  -0.78684179
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cherry      0.25788694
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cranberry  -0.03882628
## beta.Generation_Fruit_s_Treatment29_Strawberry_Strawberry -0.07259799
## sigma.Line                                                 0.10842845
##                                                               X97.5..
## beta.Generation_Fruit_s_Treatment0_GF_Cherry              -1.56273122
## beta.Generation_Fruit_s_Treatment0_GF_Cranberry           -1.32264298
## beta.Generation_Fruit_s_Treatment0_GF_Strawberry          -1.11404225
## beta.Generation_Fruit_s_Treatment7_Cherry_Cherry          -0.05547379
## beta.Generation_Fruit_s_Treatment7_Cherry_Cranberry       -0.22127165
## beta.Generation_Fruit_s_Treatment7_Cherry_Strawberry      -0.11239003
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cherry        0.26407000
## beta.Generation_Fruit_s_Treatment7_Cranberry_Cranberry     0.23133315
## beta.Generation_Fruit_s_Treatment7_Cranberry_Strawberry    0.42551421
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cherry       0.45590341
## beta.Generation_Fruit_s_Treatment7_Strawberry_Cranberry   -0.03784079
## beta.Generation_Fruit_s_Treatment7_Strawberry_Strawberry   0.02296894
## beta.Generation_Fruit_s_Treatment29_Cherry_Cherry          0.92373014
## beta.Generation_Fruit_s_Treatment29_Cherry_Cranberry      -0.12156588
## beta.Generation_Fruit_s_Treatment29_Cherry_Strawberry      0.09176605
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cherry       0.86224582
## beta.Generation_Fruit_s_Treatment29_Cranberry_Cranberry    0.44781044
## beta.Generation_Fruit_s_Treatment29_Cranberry_Strawberry  -0.23807699
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cherry      0.78225259
## beta.Generation_Fruit_s_Treatment29_Strawberry_Cranberry   0.50173417
## beta.Generation_Fruit_s_Treatment29_Strawberry_Strawberry  0.43955544
## sigma.Line                                                 0.20692044
## G0: no difference among different fruit media (beta.TreatmentrelCherry / beta.TreatmentrelCranberry include zero)
## G7: no fitness change for strawberry, cherry or cranberry populations

## G29: fitness increase for cherry populations on cherry and cranberry medium, but not on strawberry medium

## G29: fitness increase for cranberry populations on cranberry and cherry media, and fitness decrease on strawberry medium

## G29: strawberry population with no change on strawberry or cranberry medium and fitness decrease on cherry medium


m3 <- lme4::glmer(cbind(Nb_adults, Nb_eggs) ~ -1 + Generation*Fruit_s*Treatmentrel + (1|Line), data = data, verbose=TRUE, family="binomial")
## start par. =  1 fn =  27096.05 
## At return
## eval:  23 fn:      27033.634 par: 0.180886
## (NM) 20: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 40: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 60: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 80: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 100: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 120: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 140: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 160: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 180: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 200: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 220: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 240: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 260: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 280: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 300: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 320: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 340: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 360: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 380: f = 27033.6 at   0.180886   -1.32619   -1.49178    -1.7086   0.407622   0.210982   -0.46256  -0.220921  -0.758842   0.133563   0.783194  0.0748633  -0.181812  -0.145712  -0.203486   0.333268 -0.0226744  0.0987452     0.4741  -0.776315   0.911084    0.15864
## (NM) 400: f = 27033.6 at   0.180975   -1.32567   -1.49179   -1.70908   0.407121   0.211137  -0.462678  -0.220981  -0.758819   0.133482   0.783267  0.0751058  -0.181821  -0.145586  -0.203671   0.332923 -0.0226064  0.0985232    0.47445  -0.775846   0.911238    0.15907
## (NM) 420: f = 27033.6 at  0.180802  -1.32625  -1.49193  -1.70917  0.407163  0.211173 -0.462685  -0.22092 -0.758621  0.133159  0.783288  0.075011 -0.181814 -0.145684 -0.203698  0.333125 -0.022736 0.0985276  0.474286 -0.776111  0.911144  0.158836
## (NM) 440: f = 27033.6 at   0.180901   -1.32601   -1.49181    -1.7089   0.407427   0.211097  -0.462609   -0.22093  -0.758758   0.133505   0.783215  0.0749241  -0.181821  -0.145672  -0.203573   0.333202 -0.0226902  0.0986628   0.474219  -0.776181   0.911128   0.158779
## (NM) 460: f = 27033.6 at   0.180859   -1.32566   -1.49195   -1.70924   0.407258   0.211204  -0.462595  -0.220915  -0.758472   0.133407   0.783244  0.0749406  -0.181816   -0.14565  -0.203675   0.333147 -0.0227284  0.0986546   0.474307  -0.776098   0.911122   0.158852
## (NM) 480: f = 27033.6 at   0.180835   -1.32633   -1.49194   -1.70916   0.407623   0.211003  -0.462559  -0.220887  -0.758799    0.13352   0.783273  0.0747903   -0.18176  -0.145673  -0.203634   0.333275 -0.0227903  0.0986454   0.474116   -0.77631   0.911096   0.158639
## (NM) 500: f = 27033.6 at   0.180895   -1.32617   -1.49185   -1.70929   0.407298   0.211262  -0.462622  -0.220924  -0.758655   0.133367   0.783248  0.0749432   -0.18182  -0.145673   -0.20363   0.333158 -0.0227349  0.0985906   0.474289  -0.776131   0.911157   0.158847
## (NM) 520: f = 27033.6 at  0.180869  -1.32602  -1.49193  -1.70921   0.40745   0.21122 -0.462573  -0.22091 -0.758705  0.133475   0.78324 0.0748788 -0.181793 -0.145717 -0.203621   0.33321 -0.022794 0.0986262  0.474197 -0.776246  0.911107  0.158714
## (NM) 540: f = 27033.6 at   0.180875   -1.32631   -1.49198   -1.70925   0.407417   0.211185  -0.462565  -0.220893  -0.758545   0.133501   0.783263  0.0748782  -0.181786  -0.145711  -0.203616   0.333223 -0.0227927  0.0986402   0.474181   -0.77625   0.911111   0.158721
## (NM) 560: f = 27033.6 at   0.180871   -1.32633   -1.49194   -1.70928   0.407434   0.211192  -0.462556  -0.220901  -0.758694    0.13349   0.783247  0.0748468  -0.181783  -0.145713  -0.203597   0.333222 -0.0227917  0.0986677   0.474203  -0.776276   0.911145   0.158706
## (NM) 580: f = 27033.6 at   0.180871   -1.32633   -1.49194   -1.70928   0.407434   0.211192  -0.462556  -0.220901  -0.758694    0.13349   0.783247  0.0748468  -0.181783  -0.145713  -0.203597   0.333222 -0.0227917  0.0986677   0.474203  -0.776276   0.911145   0.158706
## (NM) 600: f = 27033.6 at   0.180866   -1.32631   -1.49194   -1.70927   0.407452   0.211097  -0.462575  -0.220915  -0.758598   0.133603   0.783267  0.0749029  -0.181778  -0.145702  -0.203616   0.333195 -0.0227817  0.0986648   0.474204  -0.776231   0.911168   0.158721
## (NM) 620: f = 27033.6 at    0.18084   -1.32643   -1.49199   -1.70928   0.407442   0.211122  -0.462578  -0.220903   -0.75854   0.133457   0.783263  0.0748784  -0.181782  -0.145714  -0.203612   0.333237 -0.0228026  0.0986803   0.474204  -0.776285   0.911174   0.158687
## (NM) 640: f = 27033.6 at   0.180871   -1.32646   -1.49196    -1.7093   0.407473   0.211129  -0.462583  -0.220915  -0.758621   0.133508   0.783263  0.0749113  -0.181789  -0.145685  -0.203608   0.333203 -0.0227742   0.098659   0.474233  -0.776236     0.9112   0.158739
## (NM) 660: f = 27033.6 at   0.180884   -1.32634     -1.492   -1.70928   0.407467   0.211068  -0.462584  -0.220919  -0.758594   0.133594   0.783265  0.0749043  -0.181785  -0.145682  -0.203602   0.333203 -0.0227844  0.0986575   0.474231   -0.77625   0.911201   0.158726
## (NM) 680: f = 27033.6 at   0.180856    -1.3265   -1.49199   -1.70934   0.407518   0.211071  -0.462573  -0.220919  -0.758537   0.133588   0.783256  0.0748909  -0.181779  -0.145674  -0.203589   0.333221 -0.0228038  0.0986664   0.474205  -0.776288   0.911208   0.158694
## (NM) 700: f = 27033.6 at   0.180864    -1.3263   -1.49204   -1.70929   0.407561   0.211027   -0.46257  -0.220921  -0.758589   0.133689   0.783251  0.0748777  -0.181781  -0.145685  -0.203592   0.333236 -0.0228277  0.0986743   0.474192  -0.776307   0.911221   0.158659
## (NM) 720: f = 27033.6 at   0.180889   -1.32622   -1.49205   -1.70928   0.407533   0.211038  -0.462589  -0.220933  -0.758646   0.133669   0.783247  0.0748961  -0.181803  -0.145679  -0.203607   0.333237 -0.0228135  0.0986753    0.47425  -0.776267   0.911258   0.158709
## (NM) 740: f = 27033.6 at   0.180873   -1.32627   -1.49208   -1.70933    0.40761   0.211004  -0.462572  -0.220933  -0.758629   0.133713   0.783233  0.0748672    -0.1818  -0.145672  -0.203591   0.333266 -0.0228455  0.0986874   0.474217  -0.776329   0.911273   0.158665
## (NM) 760: f = 27033.6 at   0.180899    -1.3262   -1.49208   -1.70931   0.407644   0.210968  -0.462561  -0.220921  -0.758694   0.133756   0.783236  0.0748462  -0.181798   -0.14568  -0.203595   0.333278 -0.0228544  0.0986901   0.474206  -0.776358   0.911266   0.158666
## (NM) 780: f = 27033.6 at   0.180882   -1.32627    -1.4921   -1.70931   0.407649   0.210964  -0.462579  -0.220935  -0.758674    0.13375   0.783238  0.0748571  -0.181811  -0.145692  -0.203602   0.333289 -0.0228484  0.0987005   0.474241  -0.776368   0.911314   0.158698
## (NM) 800: f = 27033.6 at   0.180881   -1.32625    -1.4921   -1.70931   0.407646   0.210966   -0.46258  -0.220935  -0.758679   0.133749   0.783239   0.074858  -0.181811  -0.145692  -0.203603   0.333288 -0.0228481   0.098699   0.474242  -0.776366   0.911314   0.158699
## (NM) 820: f = 27033.6 at   0.180878   -1.32627   -1.49208   -1.70933   0.407651   0.210956  -0.462564  -0.220925  -0.758675   0.133742   0.783235   0.074841  -0.181798  -0.145682  -0.203586   0.333289 -0.0228445  0.0986987   0.474204  -0.776387   0.911277   0.158662
## (NM) 840: f = 27033.6 at   0.180886   -1.32623   -1.49206   -1.70932   0.407634   0.211008  -0.462565  -0.220927  -0.758741   0.133731    0.78324  0.0748481  -0.181799  -0.145691  -0.203599   0.333277 -0.0228345  0.0987028   0.474219  -0.776352   0.911278   0.158677
## (NM) 860: f = 27033.6 at   0.18088  -1.32622  -1.49209  -1.70935  0.407648  0.210977 -0.462577 -0.220937 -0.758719  0.133748  0.783226 0.0748411 -0.181812 -0.145681 -0.203593  0.333309 -0.022851 0.0987179   0.47424 -0.776379  0.911317  0.158679
## (NM) 880: f = 27033.6 at   0.180874   -1.32623   -1.49205   -1.70935   0.407651   0.211007   -0.46256  -0.220924  -0.758764   0.133727   0.783224  0.0748229  -0.181801  -0.145688  -0.203593   0.333312 -0.0228422  0.0987187   0.474208  -0.776381   0.911272   0.158658
## (NM) 900: f = 27033.6 at  0.180869  -1.32625  -1.49206  -1.70932  0.407631  0.211015 -0.462573 -0.220932 -0.758752  0.133664  0.783235  0.074834 -0.181809 -0.145696 -0.203604    0.3333  -0.02284  0.098714  0.474236 -0.776377  0.911294  0.158666
## (NM) 920: f = 27033.6 at   0.180871   -1.32622   -1.49204   -1.70934   0.407662   0.211034  -0.462566  -0.220927  -0.758797     0.1337   0.783231  0.0748282  -0.181808  -0.145703  -0.203605   0.333297 -0.0228368  0.0987071   0.474226  -0.776377   0.911287   0.158681
## (NM) 940: f = 27033.6 at   0.180879   -1.32623   -1.49204   -1.70937    0.40767   0.211039  -0.462567   -0.22093  -0.758784   0.133707   0.783229  0.0748206   -0.18181  -0.145695  -0.203596   0.333314 -0.0228366  0.0987173   0.474232  -0.776385   0.911295   0.158678
## (NM) 960: f = 27033.6 at   0.180886   -1.32617   -1.49203   -1.70934   0.407669   0.211024   -0.46257  -0.220936  -0.758818   0.133714   0.783226  0.0748261  -0.181813  -0.145692  -0.203596   0.333297 -0.0228295  0.0987159   0.474238  -0.776373   0.911295   0.158675
## (NM) 980: f = 27033.6 at   0.180886   -1.32616   -1.49202   -1.70935   0.407676    0.21103  -0.462569  -0.220937  -0.758829   0.133708   0.783226  0.0748218  -0.181814  -0.145692  -0.203595   0.333299 -0.0228286  0.0987175   0.474238  -0.776376   0.911295   0.158674
## (NM) 1000: f = 27033.6 at   0.180873   -1.32619   -1.49201   -1.70937   0.407693   0.211065  -0.462566  -0.220935  -0.758844   0.133651   0.783227  0.0748046  -0.181814  -0.145697   -0.20359   0.333312 -0.0228238  0.0987228   0.474232    -0.7764   0.911293   0.158658
## (NM) 1020: f = 27033.6 at  0.180878  -1.32621    -1.492  -1.70937  0.407719  0.211081 -0.462562 -0.220933 -0.758891  0.133658  0.783228  0.074796 -0.181817 -0.145707 -0.203591  0.333321 -0.022821 0.0987313   0.47423 -0.776406   0.91129  0.158655
## (NM) 1040: f = 27033.6 at   0.180873   -1.32621   -1.49201   -1.70936   0.407713   0.211066  -0.462568   -0.22093  -0.758885    0.13367    0.78323  0.0748125  -0.181818  -0.145717  -0.203604   0.333315 -0.0228141  0.0987198   0.474228  -0.776401   0.911276   0.158661
## (NM) 1060: f = 27033.6 at   0.180874   -1.32623   -1.49201   -1.70937   0.407749   0.211067  -0.462566  -0.220935  -0.758913   0.133672   0.783227  0.0747944  -0.181824  -0.145718  -0.203597   0.333339 -0.0228206  0.0987393   0.474231  -0.776433   0.911295   0.158651
## (NM) 1080: f = 27033.6 at   0.18087  -1.32623    -1.492  -1.70939  0.407736  0.211089 -0.462569 -0.220937 -0.758907  0.133644  0.783228 0.0748008 -0.181825  -0.14572 -0.203601  0.333337  -0.02281 0.0987412  0.474237 -0.776425  0.911292  0.158656
## (NM) 1100: f = 27033.6 at   0.180876   -1.32624   -1.49199   -1.70938    0.40773   0.211101  -0.462573  -0.220939  -0.758929   0.133622   0.783233  0.0748061  -0.181829   -0.14573  -0.203605   0.333331 -0.0227898  0.0987422    0.47424  -0.776419   0.911274   0.158657
## (NM) 1120: f = 27033.6 at    0.18087   -1.32627     -1.492   -1.70938   0.407715   0.211096  -0.462574  -0.220934  -0.758873   0.133615   0.783233  0.0748157  -0.181826  -0.145724  -0.203598   0.333325 -0.0227904  0.0987307    0.47423  -0.776417   0.911264   0.158659
## (NM) 1140: f = 27033.6 at   0.180876   -1.32626   -1.49198    -1.7094   0.407698   0.211123  -0.462582  -0.220939  -0.758875   0.133576   0.783238  0.0748251  -0.181831   -0.14573  -0.203603   0.333326 -0.0227626  0.0987408   0.474242  -0.776403    0.91125   0.158663
## (NM) 1160: f = 27033.6 at   0.180884   -1.32628   -1.49199    -1.7094   0.407736   0.211109  -0.462571  -0.220934  -0.758898   0.133612   0.783236  0.0748093   -0.18183  -0.145731  -0.203593    0.33334 -0.0227666   0.098749   0.474222  -0.776428   0.911241   0.158647
## (NM) 1180: f = 27033.6 at   0.180882   -1.32627   -1.49201   -1.70941   0.407739   0.211094  -0.462577  -0.220936  -0.758878   0.133605   0.783235  0.0748142  -0.181832  -0.145728  -0.203594   0.333344 -0.0227672  0.0987505   0.474227  -0.776434   0.911249    0.15865
## (NM) 1200: f = 27033.6 at   0.180882   -1.32627   -1.49201   -1.70941   0.407739   0.211094  -0.462577  -0.220936  -0.758878   0.133605   0.783235  0.0748142  -0.181832  -0.145728  -0.203594   0.333344 -0.0227672  0.0987505   0.474227  -0.776434   0.911249    0.15865
## (NM) 1220: f = 27033.6 at   0.180884   -1.32627   -1.49201   -1.70942   0.407739   0.211091  -0.462576  -0.220934  -0.758882   0.133602   0.783236  0.0748136  -0.181832  -0.145729  -0.203594   0.333343 -0.0227647  0.0987512   0.474223  -0.776438   0.911243   0.158649
## (NM) 1240: f = 27033.6 at  0.180886  -1.32631  -1.49198  -1.70943  0.407742  0.211114 -0.462573 -0.220932 -0.758914  0.133566  0.783238 0.0748056 -0.181833 -0.145734 -0.203588  0.333342 -0.022747 0.0987542  0.474217 -0.776445  0.911224  0.158642
## (NM) 1260: f = 27033.6 at   0.18089  -1.32628  -1.49198  -1.70942  0.407742  0.211109  -0.46257  -0.22093 -0.758923  0.133573  0.783239 0.0748051 -0.181831 -0.145735 -0.203592  0.333339 -0.022749 0.0987561  0.474211 -0.776442  0.911217  0.158641
## (NM) 1280: f = 27033.6 at   0.180886   -1.32628   -1.49198   -1.70944    0.40775   0.211116  -0.462573  -0.220931  -0.758935   0.133568   0.783237  0.0748065  -0.181835   -0.14574  -0.203592   0.333345 -0.0227446  0.0987589   0.474213  -0.776447   0.911216   0.158643
## (NM) 1300: f = 27033.6 at    0.18089   -1.32629   -1.49199   -1.70943   0.407744   0.211101  -0.462573  -0.220932  -0.758926   0.133576    0.78324   0.074815  -0.181833  -0.145737   -0.20359   0.333335 -0.0227404  0.0987586    0.47421  -0.776441   0.911214   0.158642
## (NM) 1320: f = 27033.6 at   0.180884   -1.32627   -1.49199   -1.70942   0.407747   0.211097  -0.462571  -0.220931  -0.758927   0.133592   0.783238  0.0748145  -0.181831  -0.145736  -0.203592   0.333333 -0.0227535  0.0987524   0.474211   -0.77644   0.911225   0.158644
## (NM) 1340: f = 27033.6 at   0.180881   -1.32627   -1.49199   -1.70943   0.407747     0.2111  -0.462572  -0.220931  -0.758932   0.133577   0.783239  0.0748147  -0.181832  -0.145739  -0.203593   0.333335 -0.0227507  0.0987563   0.474212  -0.776443   0.911224   0.158643
## (NM) 1360: f = 27033.6 at   0.180884   -1.32626   -1.49198   -1.70943   0.407753    0.21111  -0.462572  -0.220933  -0.758932   0.133561   0.783237  0.0748152  -0.181833  -0.145735  -0.203583   0.333334 -0.0227397  0.0987613   0.474208  -0.776442   0.911215   0.158637
## (NM) 1380: f = 27033.6 at   0.180881   -1.32625   -1.49198   -1.70943   0.407753   0.211103  -0.462571  -0.220931  -0.758935   0.133578   0.783237  0.0748181  -0.181832  -0.145737  -0.203585   0.333331 -0.0227449  0.0987585   0.474207  -0.776439   0.911218    0.15864
## (NM) 1400: f = 27033.6 at   0.18088  -1.32624  -1.49197  -1.70944  0.407758  0.211126 -0.462572 -0.220935 -0.758942  0.133553  0.783235 0.0748227 -0.181833 -0.145736 -0.203577  0.333328 -0.022732 0.0987672  0.474208  -0.77643  0.911212  0.158637
## (NM) 1420: f = 27033.6 at   0.180876   -1.32621   -1.49197   -1.70943    0.40776    0.21112  -0.462571  -0.220934  -0.758964   0.133553   0.783235  0.0748175  -0.181834   -0.14574  -0.203582    0.33333 -0.0227379   0.098767   0.474208  -0.776438   0.911218   0.158637
## (NM) 1440: f = 27033.6 at   0.180875   -1.32623   -1.49197   -1.70943   0.407748   0.211126  -0.462573  -0.220934  -0.758945   0.133548   0.783237  0.0748264  -0.181832  -0.145741  -0.203583   0.333324 -0.0227338  0.0987644   0.474208  -0.776426    0.91121   0.158639
## (NM) 1460: f = 27033.6 at   0.180874   -1.32622   -1.49197   -1.70943   0.407755   0.211121   -0.46257  -0.220933  -0.758956   0.133545   0.783236  0.0748238  -0.181831   -0.14574   -0.20358   0.333322 -0.0227355  0.0987657   0.474203  -0.776429    0.91121   0.158634
## (NM) 1480: f = 27033.6 at  0.180874  -1.32622  -1.49197  -1.70943  0.407744  0.211123 -0.462573 -0.220934  -0.75895  0.133536  0.783238 0.0748259 -0.181833 -0.145743 -0.203585  0.333323 -0.022734 0.0987637  0.474209 -0.776428  0.911209  0.158637
## (NM) 1500: f = 27033.6 at   0.180876   -1.32624   -1.49197   -1.70943   0.407745   0.211129  -0.462573  -0.220933  -0.758942   0.133537   0.783238  0.0748262  -0.181831  -0.145741  -0.203582   0.333323 -0.0227327  0.0987642   0.474208  -0.776423   0.911206   0.158635
## (NM) 1520: f = 27033.6 at   0.180873   -1.32623   -1.49197   -1.70944   0.407757    0.21113  -0.462572  -0.220934  -0.758964   0.133528   0.783237  0.0748222  -0.181833  -0.145744   -0.20358   0.333328 -0.0227268  0.0987703   0.474205  -0.776433   0.911203    0.15863
## (NM) 1540: f = 27033.6 at   0.180873   -1.32623   -1.49197   -1.70944   0.407753   0.211139  -0.462571  -0.220932  -0.758965   0.133509   0.783236  0.0748207  -0.181833  -0.145746  -0.203577   0.333329 -0.0227237  0.0987731   0.474203  -0.776432   0.911195   0.158627
## (NM) 1560: f = 27033.6 at   0.180872   -1.32624   -1.49197   -1.70944   0.407753   0.211137   -0.46257  -0.220931  -0.758965   0.133509   0.783238  0.0748202  -0.181832  -0.145748  -0.203579    0.33333 -0.0227263  0.0987747   0.474202  -0.776433   0.911196   0.158624
## (NM) 1580: f = 27033.6 at   0.180874   -1.32626   -1.49198   -1.70944   0.407758    0.21113   -0.46257   -0.22093  -0.758965   0.133515   0.783239  0.0748209  -0.181832   -0.14575   -0.20358   0.333332 -0.0227231   0.098775     0.4742  -0.776438   0.911193   0.158622
## (NM) 1600: f = 27033.6 at   0.180873   -1.32623   -1.49197   -1.70944   0.407758   0.211139   -0.46257   -0.22093  -0.758979   0.133498   0.783238  0.0748203  -0.181832  -0.145752  -0.203579   0.333328 -0.0227181  0.0987765   0.474198  -0.776433   0.911185    0.15862
## (NM) 1620: f = 27033.6 at   0.180872   -1.32625   -1.49198   -1.70944    0.40776   0.211138  -0.462568   -0.22093  -0.758978   0.133497   0.783239  0.0748195  -0.181831  -0.145753  -0.203578    0.33333 -0.0227195  0.0987798   0.474198  -0.776433   0.911185   0.158615
## (NM) 1640: f = 27033.6 at   0.180873   -1.32623   -1.49198   -1.70944   0.407755   0.211129  -0.462573  -0.220931  -0.758955   0.133517   0.783236   0.074823  -0.181831  -0.145745  -0.203577   0.333327 -0.0227258  0.0987719   0.474202  -0.776426   0.911195   0.158625
## (NM) 1660: f = 27033.6 at  0.180876  -1.32624  -1.49198  -1.70944  0.407764  0.211134  -0.46257  -0.22093 -0.758977  0.133505  0.783237 0.0748167 -0.181831 -0.145752 -0.203578  0.333332 -0.022719 0.0987787  0.474199 -0.776434  0.911185  0.158617
## (NM) 1680: f = 27033.6 at  0.180877  -1.32625  -1.49199  -1.70944  0.407769  0.211128  -0.46257  -0.22093 -0.758972  0.133516  0.783238 0.0748193 -0.181829 -0.145754 -0.203576  0.333331  -0.02272 0.0987807  0.474199 -0.776427  0.911185  0.158615
## (NM) 1700: f = 27033.6 at   0.180877   -1.32622   -1.49199   -1.70945   0.407801   0.211132  -0.462569  -0.220932  -0.759018   0.133513   0.783235  0.0748139  -0.181829   -0.14576  -0.203574   0.333332 -0.0227143  0.0987893   0.474196  -0.776427   0.911183   0.158608
## (NM) 1720: f = 27033.6 at   0.180881   -1.32622     -1.492   -1.70945   0.407797   0.211123  -0.462572  -0.220931  -0.758992    0.13352   0.783233  0.0748175  -0.181829  -0.145756  -0.203569   0.333332 -0.0227138  0.0987873   0.474199  -0.776419   0.911181    0.15861
## (NM) 1740: f = 27033.6 at   0.180881   -1.32621   -1.49199   -1.70945   0.407798   0.211125  -0.462571  -0.220932  -0.758995   0.133534   0.783233  0.0748163  -0.181829  -0.145756  -0.203573   0.333332 -0.0227179  0.0987835     0.4742   -0.77642   0.911187   0.158616
## (NM) 1760: f = 27033.6 at   0.180877   -1.32621   -1.49199   -1.70946    0.40781   0.211133  -0.462571  -0.220931  -0.759015   0.133514   0.783232  0.0748161   -0.18183  -0.145763  -0.203568   0.333335 -0.0227107  0.0987927   0.474197  -0.776422   0.911179   0.158608
## (NM) 1780: f = 27033.6 at   0.180877   -1.32621   -1.49199   -1.70946    0.40781   0.211133  -0.462571  -0.220931  -0.759015   0.133514   0.783232  0.0748161   -0.18183  -0.145763  -0.203568   0.333335 -0.0227107  0.0987927   0.474197  -0.776422   0.911179   0.158608
## (NM) 1800: f = 27033.6 at   0.180877   -1.32621   -1.49199   -1.70946    0.40781   0.211133  -0.462571  -0.220931  -0.759015   0.133514   0.783232  0.0748161   -0.18183  -0.145763  -0.203568   0.333335 -0.0227107  0.0987927   0.474197  -0.776422   0.911179   0.158608
## (NM) 1820: f = 27033.6 at   0.180878   -1.32621   -1.49199   -1.70946   0.407813   0.211141  -0.462572  -0.220933  -0.759016   0.133517   0.783231  0.0748106  -0.181829   -0.14576  -0.203569   0.333338 -0.0227153  0.0987915   0.474202  -0.776419   0.911187    0.15861
## (NM) 1840: f = 27033.6 at   0.180878   -1.32621   -1.49199   -1.70946   0.407815   0.211141  -0.462572  -0.220933  -0.759017   0.133517    0.78323  0.0748105  -0.181829  -0.145761  -0.203569   0.333338 -0.0227149   0.098792   0.474203  -0.776418   0.911187    0.15861
## (NM) 1860: f = 27033.6 at   0.180877   -1.32622   -1.49199   -1.70946    0.40782   0.211147  -0.462571  -0.220932  -0.759028   0.133506   0.783232  0.0748102  -0.181829  -0.145766  -0.203568   0.333339 -0.0227089  0.0987952     0.4742  -0.776419   0.911179   0.158606
## (NM) 1880: f = 27033.6 at   0.180876   -1.32621   -1.49199   -1.70947   0.407824   0.211148  -0.462571  -0.220932  -0.759029   0.133505   0.783231  0.0748104   -0.18183  -0.145767  -0.203567   0.333341 -0.0227072  0.0987965     0.4742  -0.776423   0.911179   0.158607
## (NM) 1900: f = 27033.6 at   0.180876   -1.32623   -1.49199   -1.70947   0.407818   0.211148  -0.462572  -0.220931  -0.759022   0.133498   0.783233  0.0748135  -0.181831  -0.145768  -0.203567    0.33334 -0.0227033  0.0987952   0.474199  -0.776422   0.911174   0.158607
## (NM) 1920: f = 27033.6 at   0.180874   -1.32622   -1.49198   -1.70947   0.407816   0.211155  -0.462572   -0.22093  -0.759024   0.133491   0.783231  0.0748075  -0.181831  -0.145767  -0.203568   0.333342 -0.0227072  0.0987924     0.4742  -0.776424   0.911176    0.15861
summary(m3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: 
## cbind(Nb_adults, Nb_eggs) ~ -1 + Generation * Fruit_s * Treatmentrel +  
##     (1 | Line)
##    Data: data
## 
##      AIC      BIC   logLik deviance df.resid 
##  27077.6  27196.9 -13516.8  27033.6     1652 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -8.2174 -2.2315  0.0267  2.2418 17.5345 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  Line   (Intercept) 0.03272  0.1809  
## Number of obs: 1674, groups:  Line, 26
## 
## Fixed effects:
##                                                      Estimate Std. Error
## Generation0                                          -1.32622    0.18209
## Generation29                                         -1.49198    0.10661
## Generation7                                          -1.70947    0.09881
## Fruit_sCranberry                                      0.40782    0.13255
## Fruit_sStrawberry                                     0.21116    0.13061
## TreatmentrelCherry                                   -0.46257    0.03015
## TreatmentrelCranberry                                -0.22093    0.03010
## Generation29:Fruit_sCranberry                        -0.75902    0.18939
## Generation29:Fruit_sStrawberry                        0.13349    0.19924
## Generation29:TreatmentrelCherry                       0.78323    0.04268
## Generation7:TreatmentrelCherry                        0.07481    0.06104
## Generation29:TreatmentrelCranberry                   -0.18183    0.04520
## Generation7:TreatmentrelCranberry                    -0.14577    0.06348
## Fruit_sCranberry:TreatmentrelCherry                  -0.20357    0.07256
## Fruit_sStrawberry:TreatmentrelCherry                  0.33334    0.06141
## Fruit_sCranberry:TreatmentrelCranberry               -0.02271    0.07132
## Fruit_sStrawberry:TreatmentrelCranberry               0.09879    0.06456
## Generation29:Fruit_sCranberry:TreatmentrelCherry      0.47420    0.08272
## Generation29:Fruit_sStrawberry:TreatmentrelCherry    -0.77642    0.07473
## Generation29:Fruit_sCranberry:TreatmentrelCranberry   0.91118    0.08292
## Generation29:Fruit_sStrawberry:TreatmentrelCranberry  0.15861    0.07833
##                                                      z value Pr(>|z|)    
## Generation0                                           -7.283 3.26e-13 ***
## Generation29                                         -13.994  < 2e-16 ***
## Generation7                                          -17.300  < 2e-16 ***
## Fruit_sCranberry                                       3.077  0.00209 ** 
## Fruit_sStrawberry                                      1.617  0.10594    
## TreatmentrelCherry                                   -15.342  < 2e-16 ***
## TreatmentrelCranberry                                 -7.341 2.13e-13 ***
## Generation29:Fruit_sCranberry                         -4.008 6.13e-05 ***
## Generation29:Fruit_sStrawberry                         0.670  0.50285    
## Generation29:TreatmentrelCherry                       18.352  < 2e-16 ***
## Generation7:TreatmentrelCherry                         1.226  0.22035    
## Generation29:TreatmentrelCranberry                    -4.023 5.75e-05 ***
## Generation7:TreatmentrelCranberry                     -2.296  0.02165 *  
## Fruit_sCranberry:TreatmentrelCherry                   -2.806  0.00502 ** 
## Fruit_sStrawberry:TreatmentrelCherry                   5.428 5.69e-08 ***
## Fruit_sCranberry:TreatmentrelCranberry                -0.318  0.75018    
## Fruit_sStrawberry:TreatmentrelCranberry                1.530  0.12594    
## Generation29:Fruit_sCranberry:TreatmentrelCherry       5.733 9.89e-09 ***
## Generation29:Fruit_sStrawberry:TreatmentrelCherry    -10.390  < 2e-16 ***
## Generation29:Fruit_sCranberry:TreatmentrelCranberry   10.988  < 2e-16 ***
## Generation29:Fruit_sStrawberry:TreatmentrelCranberry   2.025  0.04287 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## fit warnings:
## fixed-effect model matrix is rank deficient so dropping 15 columns / coefficients

4 CORRELATED RESPONSES

4.1 Analysis MA and correlation

# ##### Create empty dataframe with slopes and confidence interval for all pairwise
Estimates_pairwise <- data.frame(Variables = rep(rep(c("correlation","cor_CI_inf","cor_CI_sup",
                                                       "slope", "intercept"),2),3),
                                 Generation = rep(c(rep(7, 5), rep(29, 5)),3),
                                 Pairwise = rep(c("Cherry_Cranberry",
                                                     "Cranberry_Strawberry", 
                                                     "Strawberry_Cherry"), each=10),
                                Estimates = NA)



#########################
######################### G7
#########################

####### 
#######  Cherry Cranberry
####### 

## Compute weighted correlation
weightedcor <- sjstats:::weighted_correlation(TEMP_dataG7_CheCran, x=logchange_symp, y=logchange_allop, weights=N_sumsympallop, ci.lvl = 0.95)

## With cor.tes, the estimation of correlation coefficient is right, but the confidence interval is wrong
cor.test(x=TEMP_dataG7_CheCran[rep(1:nrow(TEMP_dataG7_CheCran), TEMP_dataG7_CheCran$N_sumsympallop), "logchange_symp"], y=TEMP_dataG7_CheCran[rep(1:nrow(TEMP_dataG7_CheCran), TEMP_dataG7_CheCran$N_sumsympallop), "logchange_allop"])
## 
##  Pearson's product-moment correlation
## 
## data:  TEMP_dataG7_CheCran[rep(1:nrow(TEMP_dataG7_CheCran), TEMP_dataG7_CheCran$N_sumsympallop), "logchange_symp"] and TEMP_dataG7_CheCran[rep(1:nrow(TEMP_dataG7_CheCran), TEMP_dataG7_CheCran$N_sumsympallop), "logchange_allop"]
## t = 6.3642, df = 136, p-value = 2.79e-09
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.3390707 0.5982491
## sample estimates:
##       cor 
## 0.4790334
## Compute Major Axis regression
model <- lmodel2::lmodel2(logchange_allop ~ logchange_symp,
                            range.y = "interval",range.x = "interval",
                            data = TEMP_dataG7_CheCran[rep(1:nrow(TEMP_dataG7_CheCran), TEMP_dataG7_CheCran$N_sumsympallop),], nperm=0)

# Fill in data
Estimates_pairwise <- fillout_estimate_correlation (Estimates_pairwise, 7, "Cherry_Cranberry")


####### 
#######  Cranberry Strawberry
####### 

## Compute weighted correlation
weightedcor <- sjstats:::weighted_correlation(TEMP_dataG7_CranStraw, x=logchange_symp, y=logchange_allop, weights=N_sumsympallop, ci.lvl = 0.95)

## Compute Major Axis regression
model <- lmodel2::lmodel2(logchange_allop ~ logchange_symp,
                            range.y = "interval",range.x = "interval",
                            data = TEMP_dataG7_CranStraw[rep(1:nrow(TEMP_dataG7_CranStraw), TEMP_dataG7_CranStraw$N_sumsympallop),], nperm=0)


# Fill in data
Estimates_pairwise <- fillout_estimate_correlation (Estimates_pairwise, 7, "Cranberry_Strawberry")


####### 
#######  Strawberry  Cherry
####### 

## Compute weighted correlation
weightedcor <- sjstats:::weighted_correlation(TEMP_dataG7_StrawChe, x=logchange_symp, y=logchange_allop, weights=N_sumsympallop, ci.lvl = 0.95)

## Compute Major Axis regression
model <- lmodel2::lmodel2(logchange_allop ~ logchange_symp,
                            range.y = "interval",range.x = "interval",
                            data = TEMP_dataG7_StrawChe[rep(1:nrow(TEMP_dataG7_StrawChe), TEMP_dataG7_StrawChe$N_sumsympallop),], nperm=0)

# Fill in data
Estimates_pairwise <- fillout_estimate_correlation (Estimates_pairwise, 7, "Strawberry_Cherry")


#########################
######################### G29
######################### 


####### 
#######  Cherry Cranberry
####### 


## Compute unweighted correlation
unweightedcor <- cor.test(x=TEMP_dataG29_CheCran$logchange_symp, y=TEMP_dataG29_CheCran$logchange_allop)

## Compute weighted correlation
weightedcor <- sjstats:::weighted_correlation(TEMP_dataG29_CheCran, x=logchange_symp, y=logchange_allop, weights=N_sumsympallop, ci.lvl = 0.95)

## Compute Major Axis regression
model <- lmodel2::lmodel2(logchange_allop ~ logchange_symp,
                            range.y = "interval",range.x = "interval",
                            data = TEMP_dataG29_CheCran[rep(1:nrow(TEMP_dataG29_CheCran), TEMP_dataG29_CheCran$N_sumsympallop),], nperm=0)

# Fill in data
Estimates_pairwise <- fillout_estimate_correlation (Estimates_pairwise, 29, "Cherry_Cranberry")


####### 
#######  Cranberry Strawberry
####### 

## Compute unweighted correlation
unweightedcor <- cor.test(x=TEMP_dataG29_CranStraw$logchange_symp, y=TEMP_dataG29_CranStraw$logchange_allop)

## Compute weighted correlation
weightedcor <- sjstats:::weighted_correlation(TEMP_dataG29_CranStraw, x=logchange_symp, y=logchange_allop, weights=N_sumsympallop, ci.lvl = 0.95)

## Compute Major Axis regression
model <- lmodel2::lmodel2(logchange_allop ~ logchange_symp,
                            range.y = "interval",range.x = "interval",
                            data = TEMP_dataG29_CranStraw[rep(1:nrow(TEMP_dataG29_CranStraw), TEMP_dataG29_CranStraw$N_sumsympallop),], nperm=0)


# Fill in data
Estimates_pairwise <- fillout_estimate_correlation (Estimates_pairwise, 29, "Cranberry_Strawberry")


####### 
#######  Strawberry  Cherry
####### 

## Compute unweighted correlation
unweightedcor <- cor.test(x=TEMP_dataG29_StrawChe$logchange_symp, y=TEMP_dataG29_StrawChe$logchange_allop)

## Compute weighted correlation
weightedcor <- sjstats:::weighted_correlation(TEMP_dataG29_StrawChe, x=logchange_symp, y=logchange_allop, weights=N_sumsympallop, ci.lvl = 0.95)

## Compute Major Axis regression
model <- lmodel2::lmodel2(logchange_allop ~ logchange_symp,
                            range.y = "interval",range.x = "interval",
                            data = TEMP_dataG29_StrawChe[rep(1:nrow(TEMP_dataG29_StrawChe), TEMP_dataG29_StrawChe$N_sumsympallop),], nperm=0)

# Fill in data
Estimates_pairwise <- fillout_estimate_correlation (Estimates_pairwise, 29, "Strawberry_Cherry")


## Some estimates are outside [-1, 1]
Estimates_pairwise[Estimates_pairwise$Variables=="cor_CI_inf",]
##     Variables Generation             Pairwise    Estimates
## 2  cor_CI_inf          7     Cherry_Cranberry  0.001431407
## 7  cor_CI_inf         29     Cherry_Cranberry -1.051419194
## 12 cor_CI_inf          7 Cranberry_Strawberry  0.105487696
## 17 cor_CI_inf         29 Cranberry_Strawberry  0.120501768
## 22 cor_CI_inf          7    Strawberry_Cherry  0.188354905
## 27 cor_CI_inf         29    Strawberry_Cherry  0.145604434
Estimates_pairwise[Estimates_pairwise$Variables=="cor_CI_sup",]
##     Variables Generation             Pairwise   Estimates
## 3  cor_CI_sup          7     Cherry_Cranberry  0.95663530
## 8  cor_CI_sup         29     Cherry_Cranberry -0.08369498
## 13 cor_CI_sup          7 Cranberry_Strawberry  0.96523247
## 18 cor_CI_sup         29 Cranberry_Strawberry  1.06648658
## 23 cor_CI_sup          7    Strawberry_Cherry  1.04499320
## 28 cor_CI_sup         29    Strawberry_Cherry  1.20639674
## Change the estimate to the boundary
Estimates_pairwise$Estimates[Estimates_pairwise$Variables=="cor_CI_inf"&Estimates_pairwise$Estimates<(-1)] <- rep(-1, sum(Estimates_pairwise$Variables=="cor_CI_inf"&Estimates_pairwise$Estimates<(-1)))

Estimates_pairwise$Estimates[Estimates_pairwise$Variables=="cor_CI_sup"&Estimates_pairwise$Estimates > 1] <- rep(1, sum(Estimates_pairwise$Variables=="cor_CI_sup"&Estimates_pairwise$Estimates > 1))

4.2 Compute confidence interval of the difference

computeCIcordifG7G29(pair="Cherry_Cranberry")
## correlationdifference               lowerCI              higherCI 
##             1.0465904             0.3667183             1.6908811
computeCIcordifG7G29(pair="Cranberry_Strawberry")
## correlationdifference               lowerCI              higherCI 
##           -0.05813409           -0.64977355            0.58101541
computeCIcordifG7G29(pair="Strawberry_Cherry")
## correlationdifference               lowerCI              higherCI 
##           -0.05932653           -0.59638604            0.59508836

4.3 Plot Fitness

ymin = -50
ymax = 50


##################################################
##################   G7
# Limits
ymin_CheCranG7=min(min(TEMP_dataG7_CheCran$lowCIlogfitnesschange_allop, na.rm= TRUE),
                min(TEMP_dataG7_CheCran$lowCIlogfitnesschange_symp, na.rm= TRUE)) 
ymax_CheCranG7=max(max(TEMP_dataG7_CheCran$upCIlogfitnesschange_allop, na.rm= TRUE),
                max(TEMP_dataG7_CheCran$upCIlogfitnesschange_symp, na.rm= TRUE))
lim_text<-2



# Plot
CheCran_G7 <- ggplot(data = TEMP_dataG7_CheCran) + 
  geom_abline(intercept = Estimates_pairwise$Estimates[Estimates_pairwise$Generation == 7 & 
                               Estimates_pairwise$Pairwise == "Cherry_Cranberry" & 
                               Estimates_pairwise$Variables == "intercept"],
               slope = Estimates_pairwise$Estimates[Estimates_pairwise$Generation == 7 & 
                               Estimates_pairwise$Pairwise == "Cherry_Cranberry" & 
                               Estimates_pairwise$Variables == "slope"],
              colour = "black", size = 0.75) + 
  geom_text(x = 0, y = 2, 
            label = eq_r(gen = 7, pair = "Cherry_Cranberry"), parse = TRUE, 
            color="black", size = 3.5) +
  geom_errorbar(aes(x = logchange_symp, ymin = lowCIlogfitnesschange_allop,
                    ymax = upCIlogfitnesschange_allop,
                    color = Fruit_s),
                width= 0.02, size = 0.3, alpha = 0.8) + 
  geom_errorbarh(aes(y = logchange_allop, xmin = lowCIlogfitnesschange_symp, 
                     xmax = upCIlogfitnesschange_symp, 
                     color = Fruit_s),
                 height = 0.02, size = 0.3, alpha = 0.8) + 
  geom_point(aes(x = logchange_symp, y = logchange_allop,  
                 color = Fruit_s,fill = Fruit_s, shape = Treatment),
             size = 3, fill = "white", stroke =1.2) + 
  xlab("Fitness change in\nselective environment")  + 
  ylab("Fitness change in\nalternative environment")  + 
  ggtitle("Cherry vs. Cranberry") + 
  theme_LO_sober + 
  labs(shape = "Test fruit", color = "Evolution fruit") + 
  scale_shape_manual(values = c(21, 22)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424"))  + 
  coord_cartesian(ylim = c(ymin_CheCranG7, ymax_CheCranG7), 
                  xlim = c(ymin_CheCranG7, ymax_CheCranG7)) 
  
 CheCran_G7

# Limits
ymin_CranStrawG7=min(min(TEMP_dataG7_CranStraw$lowCIlogfitnesschange_allop, na.rm= TRUE),
                min(TEMP_dataG7_CranStraw$lowCIlogfitnesschange_symp, na.rm= TRUE))
ymax_CranStrawG7=max(max(TEMP_dataG7_CranStraw$upCIlogfitnesschange_allop, na.rm= TRUE),
                max(TEMP_dataG7_CranStraw$upCIlogfitnesschange_symp, na.rm= TRUE))
lim_text<-ymin_CranStrawG7+0.99*(ymax_CranStrawG7-ymin_CranStrawG7)


CranStraw_G7 <-  ggplot(data = TEMP_dataG7_CranStraw) + 
  geom_abline(intercept = Estimates_pairwise$Estimates[Estimates_pairwise$Generation == 7 & 
                               Estimates_pairwise$Pairwise == "Cranberry_Strawberry" & 
                               Estimates_pairwise$Variables == "intercept"],
               slope = Estimates_pairwise$Estimates[Estimates_pairwise$Generation == 7 & 
                               Estimates_pairwise$Pairwise == "Cranberry_Strawberry" & 
                               Estimates_pairwise$Variables == "slope"],
              colour = "black", size = 0.75) + 
  geom_errorbar(aes(x = logchange_symp, ymin = lowCIlogfitnesschange_allop, 
                    ymax = upCIlogfitnesschange_allop,
                    color = Fruit_s),
                  width= 0.02, size = 0.3, alpha = 0.8) + 
  geom_errorbarh(aes(y = logchange_allop, xmin = lowCIlogfitnesschange_symp, 
                     xmax = upCIlogfitnesschange_allop, 
                 color = Fruit_s),
                 height = 0.02, size = 0.3, alpha = 0.8) + 
  geom_point(aes(x =logchange_symp, y = logchange_allop,  color = Fruit_s,
                 fill = Fruit_s, shape = Treatment),
                   size =3, fill = "white", stroke =1.2) + 
  geom_text(x = -0.25, y = lim_text, label = eq_r(gen = 7, pair = "Cranberry_Strawberry"), parse = TRUE, color="black", size = 3.5) +
  xlab("Fitness change in\nselective environment")  + 
  ylab("Fitness change in\nalternative environment")  + 
     ggtitle("Cranberry vs. Strawberry") + 
  coord_cartesian(ylim = c(ymin_CranStrawG7, ymax_CranStrawG7), 
                  xlim = c(ymin_CranStrawG7, ymax_CranStrawG7)) + 
   labs(shape = "Test fruit", color = "Selection fruit") + 
   scale_shape_manual(values = c(22, 24)) + 
   scale_color_manual(values = c("#FDB424", "#3FAA96"))  + 
  theme_LO_sober
 CranStraw_G7

# Limits
ymin_StrawCheG7=min(min(TEMP_dataG7_StrawChe$lowCIlogfitnesschange_allop, na.rm= TRUE),
                min(TEMP_dataG7_StrawChe$lowCIlogfitnesschange_symp, na.rm= TRUE))
ymax_StrawCheG7=max(max(TEMP_dataG7_StrawChe$upCIlogfitnesschange_allop, na.rm= TRUE),
                max(TEMP_dataG7_StrawChe$lowCIlogfitnesschange_symp, na.rm= TRUE))
lim_text<-ymin_StrawCheG7+0.99*(ymax_StrawCheG7-ymin_StrawCheG7)


StrawChe_G7 <-  ggplot(data = TEMP_dataG7_StrawChe) + 
  geom_abline(intercept = Estimates_pairwise$Estimates[Estimates_pairwise$Generation == 7 & 
                               Estimates_pairwise$Pairwise == "Strawberry_Cherry" & 
                               Estimates_pairwise$Variables == "intercept"],
               slope = Estimates_pairwise$Estimates[Estimates_pairwise$Generation == 7 & 
                               Estimates_pairwise$Pairwise == "Strawberry_Cherry" & 
                               Estimates_pairwise$Variables == "slope"],
              colour = "black", size = 0.75) + 
  geom_errorbar(aes(x =logchange_symp, 
                    ymin = lowCIlogfitnesschange_allop, 
                    ymax = upCIlogfitnesschange_allop,
                    color = Fruit_s),
                  width= 0.02, size = 0.3, alpha = 0.8) + 
  geom_errorbarh(aes(y = logchange_allop, 
                     xmin = lowCIlogfitnesschange_symp, 
                     xmax = upCIlogfitnesschange_symp, 
                 color = Fruit_s),
                 height = 0.02, size = 0.3, alpha = 0.8) + 
  geom_point(aes(x = logchange_symp, y = logchange_allop,  color = Fruit_s,fill = Fruit_s, shape = Treatment),
                 size =3, fill = "white", stroke =1.2) + 
  geom_text(x = -0.13, y = lim_text, label = eq_r(gen = 7, pair = "Strawberry_Cherry"), parse = TRUE, color="black", size = 3.5) +
  xlab("Fitness change in\nselective environment")  + 
  ylab("Fitness change in\nalternative environment")  + 
  ggtitle("Strawberry vs. Cherry") + 
  labs(shape = "Test fruit", color = "Selection fruit") + 
  scale_shape_manual(values = c(21, 24)) + 
  scale_color_manual(values = c("#BC3C6D", "#3FAA96"))  + 
  coord_cartesian(ylim = c(ymin_StrawCheG7, ymax_StrawCheG7), 
                  xlim = c(ymin_StrawCheG7, ymax_StrawCheG7)) + 
  theme_LO_sober
 StrawChe_G7

#####################################################################################
##################################      G29        ##################################
#####################################################################################
TEMP_dataG29_CheCran
##    Treatment Line   Fruit_s Generation N_symp logchange_symp
## 5     Cherry  CEA    Cherry         29     30     0.14767000
## 6     Cherry  CEB    Cherry         29     30     0.25038029
## 7     Cherry  CEC    Cherry         29     30     0.35854062
## 38 Cranberry  CRA Cranberry         29     30    -0.14885350
## 39 Cranberry  CRB Cranberry         29     30     0.29719689
## 40 Cranberry  CRC Cranberry         29     30     0.02703717
## 41 Cranberry  CRD Cranberry         29     30    -0.15627744
## 42 Cranberry  CRE Cranberry         29     30     0.72513486
##    lowCIlogfitnesschange_symp upCIlogfitnesschange_symp N_allop logchange_allop
## 5                 -0.12317382                 0.4301216      30     -0.21774071
## 6                 -0.01964191                 0.5320919      30     -0.59200204
## 7                  0.08929875                 0.6395485      30     -0.20670829
## 38                -0.42196368                 0.1356310      30      0.32537794
## 39                 0.02797352                 0.5781852      30      0.16929862
## 40                -0.24433581                 0.3099615      30      0.08474123
## 41                -0.42946769                 0.1282789      30      0.54736475
## 42                 0.45833897                 1.0039294      30     -0.10496044
##    lowCIlogfitnesschange_allop upCIlogfitnesschange_allop N_sumsympallop
## 5                  -0.49161649                 0.06743017             60
## 6                  -0.87105791                -0.30220654             60
## 7                  -0.48045799                 0.07834961             60
## 38                  0.05590556                 0.60659377             60
## 39                 -0.10136525                 0.45158825             60
## 40                 -0.18664831                 0.36768394             60
## 41                  0.27930142                 0.82730827             60
## 42                 -0.37821315                 0.17965592             60
TEMP_dataG29_CranStraw
##     Treatment Line    Fruit_s Generation N_symp logchange_symp
## 38  Cranberry  CRA  Cranberry         29     30    -0.14885350
## 39  Cranberry  CRB  Cranberry         29     30     0.29719689
## 40  Cranberry  CRC  Cranberry         29     30     0.02703717
## 41  Cranberry  CRD  Cranberry         29     30    -0.15627744
## 42  Cranberry  CRE  Cranberry         29     30     0.72513486
## 73 Strawberry  FRA Strawberry         29     30     0.15709032
## 74 Strawberry  FRB Strawberry         29     30     0.56023100
## 75 Strawberry  FRC Strawberry         29     30     0.36640738
##    lowCIlogfitnesschange_symp upCIlogfitnesschange_symp N_allop logchange_allop
## 38                -0.42196368                 0.1356310      30     -0.29731422
## 39                 0.02797352                 0.5781852      30     -0.24539416
## 40                -0.24433581                 0.3099615      30     -0.28950778
## 41                -0.42946769                 0.1282789      30     -1.02555272
## 42                 0.45833897                 1.0039294      30     -0.16810249
## 73                -0.11224578                 0.4381748      30     -0.25158762
## 74                 0.29332571                 0.8391204      30      0.04303751
## 75                 0.09845305                 0.6462450      30      0.52109773
##    lowCIlogfitnesschange_allop upCIlogfitnesschange_allop N_sumsympallop
## 38                  -0.5708280               -0.012475350             60
## 39                  -0.5183319                0.038928334             60
## 40                  -0.5629331               -0.004748193             60
## 41                  -1.3110679               -0.730048996             60
## 42                  -0.4402347                0.115497271             60
## 73                  -0.5258587                0.033937386             60
## 74                  -0.2281917                0.325832559             60
## 75                   0.2532712                0.800824675             60
TEMP_dataG29_StrawChe
##     Treatment Line    Fruit_s Generation N_symp logchange_symp
## 5      Cherry  CEA     Cherry         29     30      0.1476700
## 6      Cherry  CEB     Cherry         29     30      0.2503803
## 7      Cherry  CEC     Cherry         29     30      0.3585406
## 73 Strawberry  FRA Strawberry         29     30      0.1570903
## 74 Strawberry  FRB Strawberry         29     30      0.5602310
## 75 Strawberry  FRC Strawberry         29     30      0.3664074
##    lowCIlogfitnesschange_symp upCIlogfitnesschange_symp N_allop logchange_allop
## 5                 -0.12317382                 0.4301216      30      0.01971359
## 6                 -0.01964191                 0.5320919      30     -0.15850680
## 7                  0.08929875                 0.6395485      30      0.21131464
## 73                -0.11224578                 0.4381748      30     -0.50599095
## 74                 0.29332571                 0.8391204      30      0.19377147
## 75                 0.09845305                 0.6462450      30      0.32151693
##    lowCIlogfitnesschange_allop upCIlogfitnesschange_allop N_sumsympallop
## 5                  -0.25069640                  0.3017653             60
## 6                  -0.43054314                  0.1250069             60
## 7                  -0.05763581                  0.4920514             60
## 73                 -0.78451399                 -0.2166643             60
## 74                 -0.07669333                  0.4758818             60
## 75                  0.05201722                  0.6027574             60
# Limits
ymin_CheCranG29=min(min(TEMP_dataG29_CheCran$lowCIlogfitnesschange_allop, na.rm= TRUE),
                min(TEMP_dataG29_CheCran$lowCIlogfitnesschange_symp, na.rm= TRUE))
ymax_CheCranG29=max(max(TEMP_dataG29_CheCran$upCIlogfitnesschange_allop, na.rm= TRUE),
                max(TEMP_dataG29_CheCran$upCIlogfitnesschange_symp, na.rm= TRUE))
lim_text<-ymin_CheCranG29+0.99*(ymax_CheCranG29-ymin_CheCranG29)


CheCran_G29 <- ggplot(data = TEMP_dataG29_CheCran) + 
    geom_abline(intercept = Estimates_pairwise$Estimates[Estimates_pairwise$Generation ==29 & 
                               Estimates_pairwise$Pairwise == "Cherry_Cranberry" & 
                               Estimates_pairwise$Variables == "intercept"],
               slope = Estimates_pairwise$Estimates[Estimates_pairwise$Generation ==29 & 
                               Estimates_pairwise$Pairwise == "Cherry_Cranberry" & 
                               Estimates_pairwise$Variables == "slope"],
              colour = "black", size = 0.75) + 
  geom_errorbar(aes(x =logchange_symp, ymin = lowCIlogfitnesschange_allop, 
                    ymax = upCIlogfitnesschange_allop,
                    color = Fruit_s),
                  width= 0.02, size = 0.3, alpha = 0.8) + 
  geom_errorbarh(aes(y = logchange_allop, xmin = lowCIlogfitnesschange_symp, 
                     xmax = upCIlogfitnesschange_symp, 
                 color = Fruit_s),
                 height = 0.02, size = 0.3, alpha = 0.8) + 
  geom_point(aes(x =logchange_symp, y = logchange_allop,  color = Fruit_s, fill = Fruit_s, shape = Treatment),
                 size =3, fill = "white", stroke =1.2) + 
  geom_text(x = 0.33, y = lim_text, label = eq_r(gen = 29, pair = "Cherry_Cranberry"), parse = TRUE, color="black", size = 3.5) +
  xlab("Fitness change in\nselective environment")  + 
  ylab("Fitness change in\nalternative environment")  + 
  ggtitle("Cherry vs. Cranberry") + 
  labs(shape = "Test fruit", color = "Evolution fruit") + 
  scale_shape_manual(values = c(16, 15)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424"))  + 
  coord_cartesian(ylim = c(ymin_CheCranG29, ymax_CheCranG29), 
                  xlim = c(ymin_CheCranG29, ymax_CheCranG29)) + 
  theme_LO_sober
 CheCran_G29

# Limits
ymin_CranStrawG29=min(min(TEMP_dataG29_CranStraw$lowCIlogfitnesschange_allop, na.rm= TRUE),
                min(TEMP_dataG29_CranStraw$lowCIlogfitnesschange_symp, na.rm= TRUE))
ymax_CranStrawG29=max(max(TEMP_dataG29_CranStraw$upCIlogfitnesschange_allop, na.rm= TRUE),
                max(TEMP_dataG29_CranStraw$upCIlogfitnesschange_symp, na.rm= TRUE))
lim_text<-ymin_CranStrawG29+0.99*(ymax_CranStrawG29-ymin_CranStrawG29)

#Plot
 CranStraw_G29 <- ggplot(data = TEMP_dataG29_CranStraw) + 
  geom_abline(intercept = Estimates_pairwise$Estimates[Estimates_pairwise$Generation ==29 & 
                               Estimates_pairwise$Pairwise == "Cranberry_Strawberry" & 
                               Estimates_pairwise$Variables == "intercept"],
               slope = Estimates_pairwise$Estimates[Estimates_pairwise$Generation ==29 & 
                               Estimates_pairwise$Pairwise == "Cranberry_Strawberry" & 
                               Estimates_pairwise$Variables == "slope"],
              colour = "black", size = 0.75) + 
   geom_errorbar(aes(x =logchange_symp, ymin = lowCIlogfitnesschange_allop, 
                    ymax = upCIlogfitnesschange_allop,
                    color = Fruit_s),
                  width= 0.02, size = 0.3, alpha = 0.8) + 
   geom_errorbarh(aes(y = logchange_allop, xmin = lowCIlogfitnesschange_symp,
                      xmax = upCIlogfitnesschange_symp, 
                 color = Fruit_s),
                 height = 0.02, size = 0.3, alpha = 0.8) + 
   geom_point(aes(x =logchange_symp, y = logchange_allop,  
                  color = Fruit_s,fill = Fruit_s, shape = Treatment),
                 size =3, fill = "white", stroke =1.2) + 
   geom_text(x = -0.45, y = lim_text, label = eq_r(gen = 29, pair = "Cranberry_Strawberry"), parse = TRUE, color="black", size = 3.5) +
   xlab("Fitness change in\nselective environment")  + 
   ylab("Fitness change in\nalternative environment")  + 
   ggtitle("Cranberry vs. Strawberry") + 
   coord_cartesian(ylim = c(ymin_CranStrawG29, ymax_CranStrawG29), 
                  xlim = c(ymin_CranStrawG29, ymax_CranStrawG29)) + 
   labs(shape = "Test fruit", color = "Selection fruit") + 
   scale_shape_manual(values = c(15, 17)) + 
   scale_color_manual(values = c("#FDB424", "#3FAA96"))  + 
   theme_LO_sober
 CranStraw_G29

# Limits
ymin_StrawCheG29=min(min(TEMP_dataG29_StrawChe$lowCIlogfitnesschange_allop, na.rm= TRUE),
                min(TEMP_dataG29_StrawChe$lowCIlogfitnesschange_symp, na.rm= TRUE))
ymax_StrawCheG29=max(max(TEMP_dataG29_StrawChe$upCIlogfitnesschange_allop, na.rm= TRUE),
                max(TEMP_dataG29_StrawChe$upCIlogfitnesschange_symp, na.rm= TRUE))
lim_text<-ymin_StrawCheG29+0.99*(ymax_StrawCheG29-ymin_StrawCheG29)


StrawChe_G29 <- ggplot(data = TEMP_dataG29_StrawChe) + 
 geom_abline(intercept = Estimates_pairwise$Estimates[Estimates_pairwise$Generation ==29 & 
                               Estimates_pairwise$Pairwise == "Strawberry_Cherry" & 
                               Estimates_pairwise$Variables == "intercept"],
               slope = Estimates_pairwise$Estimates[Estimates_pairwise$Generation ==29 & 
                               Estimates_pairwise$Pairwise == "Strawberry_Cherry" & 
                               Estimates_pairwise$Variables == "slope"],
              colour = "black", size = 0.75) + 
  geom_errorbar(aes(x =logchange_symp, ymin = lowCIlogfitnesschange_allop, 
                    ymax = upCIlogfitnesschange_allop,
                    color = Fruit_s),
                  width= 0.02, size = 0.2, alpha =1) + 
  geom_errorbarh(aes(y = logchange_allop, xmin = lowCIlogfitnesschange_symp, 
                     xmax = upCIlogfitnesschange_symp, 
                 color = Fruit_s),
                 height = 0.02, size = 0.2, alpha =1) + 
  geom_point(aes(x =logchange_symp, y = logchange_allop,  
                 color = Fruit_s, fill = Fruit_s, shape = Treatment),
                 size =3, fill = "white", stroke =1.2) + 
  geom_text(x = -0.2, y = lim_text, 
            label = eq_r(gen = 29, pair = "Strawberry_Cherry"), 
            parse = TRUE, color="black", size = 3.5) +
  coord_cartesian(ylim = c(ymin_StrawCheG29, ymax_StrawCheG29), 
                  xlim = c(ymin_StrawCheG29, ymax_StrawCheG29)) + 
    xlab("Fitness change in\nselective environment")  + 
    ylab("Fitness change in\nalternative environment")  + 
  ggtitle("Strawberry vs. Cherry") + 
  labs(shape = "Test fruit", color = "Selection fruit") + 
  scale_shape_manual(values = c(16, 17)) + 
  scale_color_manual(values = c("#BC3C6D", "#3FAA96"))  + 
  theme_LO_sober
 StrawChe_G29

legend_tradevide <-  ggplot(data = data_logchange[data_logchange$Generation == "29",],
                          aes(x =logchange, y = logchange,  color = Fruit_s, fill = Fruit_s, 
                              shape = Treatment)) + 
  geom_point(size =2.5, fill = "white") + 
  labs(shape = "Test fruit", color = "Selection fruit") + 
  scale_shape_manual(values = c(16, 15, 17)) + 
  scale_color_manual(values = c("#BC3C6D", "#FDB424", "#3FAA96"))  + 
  theme_LO_sober

legend_trade <- lemon::g_legend(legend_tradevide)

 
 

Slopeestimates_logchange_pairwise_errorbar <- cowplot::ggdraw() + 
  cowplot::draw_plot(CheCran_G7 + theme(legend.position = "none"), 
            x = 0.01, y = 0.5, width = 0.22, height = 0.45) + 
  cowplot::draw_plot(CranStraw_G7 + theme(legend.position = "none"), 
            x = 0.31, y = 0.5, width = 0.22, height = 0.45) + 
  cowplot::draw_plot(StrawChe_G7 + theme(legend.position = "none"), 
            x = 0.61, y = 0.5, width = 0.22, height = 0.45) + 
  cowplot::draw_plot(legend_trade, x = 0.85, y = 0.5, width = 0.1, height = 0.1) + 
  cowplot::draw_plot(CheCran_G29 + theme(legend.position = "none"), 
            x = 0.01, y = 0, width = 0.22, height = 0.45) + 
  cowplot::draw_plot(CranStraw_G29 + theme(legend.position = "none"), 
            x = 0.31, y = 0, width = 0.22, height = 0.45) + 
  cowplot::draw_plot(StrawChe_G29 + theme(legend.position = "none"), 
            x = 0.61, y = 0, width = 0.22, height = 0.45) + 
  cowplot::draw_plot_label(c("Intermediate phenotyping step", "A", "B", "C", " ",
                    "Final phenotyping step", "D", "E", "F", " "),  
                  x = c(0.30, 0.01, 0.30, 0.61, 0.92, 0.250, 0.01, 0.30, 0.61, 0.92), 
                  y = c(0.99, 0.95, 0.95, 0.95, 0.95, 0.49, 0.45, 0.45, 0.45, 0.45), 
                  hjust = c(-0.25, -0.25, -0.25, -0.25, -0.25, -0.75, -0.75, -0.75, -0.75, -0.75), 
                  vjust = c(1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5),
                  size = 16) 
 Slopeestimates_logchange_pairwise_errorbar

#  
# #  
# 
cowplot::save_plot(file =here::here("figures", "FIG_Correlation.pdf"),
                   Slopeestimates_logchange_pairwise_errorbar,
                   base_height = 18/cm(1), base_width = 34/cm(1), dpi = 610)

5 SA AND LOCAL ADAPTATION

5.1 Analysis fitness change

##### 
#####  G7
##### 
lm_val_G7 = lm(logchange ~ Treatment + Line + SA + Treatment:Fruit_s, 
               weights = N, data = data_logchange[data_logchange$Generation=="7",])

Fratio = anova(lm_val_G7)[3, 3]/anova(lm_val_G7)[4, 3]
pvalue = 1 - pf(Fratio, anova(lm_val_G7)[3, 1], anova(lm_val_G7)[4, 1]) 

pvalue
## [1] 0.9671626
Fratio
## [1] 0.001997209
anova(lm_val_G7)[3, 1]
## [1] 1
anova(lm_val_G7)[4, 1]
## [1] 3
##### 
#####  G29
##### 
lm_val_G29 = lm(logchange ~ Treatment + Line + SA + Treatment:Fruit_s, 
               weights = N, data = data_logchange[data_logchange$Generation=="29",])

Fratio = anova(lm_val_G29)[3, 3]/anova(lm_val_G29)[4, 3]
pvalue = 1 - pf(Fratio, anova(lm_val_G29)[3, 1], anova(lm_val_G29)[4, 1]) 

pvalue
## [1] 0.1877934
Fratio
## [1] 2.888168
anova(lm_val_G29)[3, 1]
## [1] 1
anova(lm_val_G29)[4, 1]
## [1] 3